Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 106 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,112 @@ composer require yii2-extensions/nested-sets-behavior
3. **Optimizes queries** using boundary values for efficient tree traversal.
4. **Supports transactions** to ensure data integrity during complex operations.

## Database setup

The package includes migrations to create the necessary database tables for nested sets functionality.

### Available migrations

The package provides two migration files.

1. **Single tree migration** (`m250707_103609_tree.php`) - for single tree per table.
2. **Multiple trees migration** (`m250707_104009_multiple_tree.php`) - for multiple independent trees.

### Using package migrations

#### Method 1: Run migrations directly from extension (Recommended)

Configure your console application to include the extension migrations path.

```php
<?php

declare(strict_types=1);

use yii\console\controllers\MigrateController;

// console/config/main.php
return [
'controllerMap' => [
'migrate' => [
'class' => MigrateController::class,
'migrationPath' => [
'@console/migrations', // Your app migrations
'@vendor/yii2-extensions/nested-sets-behavior/migrations', // Extension migrations
],
],
],
];
```

Then run migrations normally.

```bash
# View available migrations (including extension migrations)
./yii migrate/history

# Run all pending migrations
./yii migrate

# Or run specific migrations
./yii migrate/up m250707_103609_tree
./yii migrate/up m250707_104009_multiple_tree
```

#### Method 2: Using migration path parameter

Run migrations directly without configuration changes.

```bash
# Run single tree migration
./yii migrate/up --migrationPath=@vendor/yii2-extensions/nested-sets-behavior/migrations

# Run specific migration from extension
./yii migrate/up m250707_103609_tree --migrationPath=@vendor/yii2-extensions/nested-sets-behavior/migrations
```

### Migration details

#### Single tree migration (`m250707_103609_tree.php`)

Creates a `tree` table with the following structure:

```sql
CREATE TABLE `tree` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`depth` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_tree_lft` (`lft`),
KEY `idx_tree_rgt` (`rgt`),
KEY `idx_tree_depth` (`depth`),
KEY `idx_tree_lft_rgt` (`lft`,`rgt`)
);
```

#### Multiple tree migration (`m250707_104009_multiple_tree.php`)

Creates a `multiple_tree` table with the following structure:

```sql
CREATE TABLE `multiple_tree` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tree` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`depth` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_multiple_tree_tree` (`tree`),
KEY `idx_multiple_tree_lft` (`lft`),
KEY `idx_multiple_tree_rgt` (`rgt`),
KEY `idx_multiple_tree_depth` (`depth`),
KEY `idx_multiple_tree_tree_lft_rgt` (`tree`,`lft`,`rgt`)
);
```

### Basic Configuration

Add the behavior to your ActiveRecord model.
Expand Down Expand Up @@ -111,31 +217,6 @@ class Category extends ActiveRecord
}
```

### Database schema

Create the required database fields.

```sql
-- Single tree structure
CREATE TABLE category (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
lft INT NOT NULL,
rgt INT NOT NULL,
depth INT NOT NULL
);

-- Multiple trees structure
CREATE TABLE category (
id INT PRIMARY KEY AUTO_INCREMENT,
tree INT,
name VARCHAR(255) NOT NULL,
lft INT NOT NULL,
rgt INT NOT NULL,
depth INT NOT NULL
);
```

### Basic Usage

#### Creating and building trees
Expand Down
8 changes: 7 additions & 1 deletion migrations/m250707_103609_tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ class m250707_103609_tree extends Migration
public function safeUp()
{
$rawPrimaryKey = 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY';
$tableOptions = [];

if ($this->db->driverName === 'mysql') {
$tableOptions = 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
}

$this->createTable(
'{{%tree}}',
[
'id' => $this->db->driverName !== 'oci' ? $this->primaryKey()->notNull() : $rawPrimaryKey,
'name' => $this->db->driverName === 'oci' ? $this->string()->notNull() : $this->text()->notNull(),
'name' => $this->string(255)->notNull(),
'lft' => $this->integer()->notNull(),
'rgt' => $this->integer()->notNull(),
'depth' => $this->integer()->notNull(),
],
$tableOptions,
);

$this->createIndex('idx_tree_lft', '{{%tree}}', 'lft');
Expand Down
7 changes: 6 additions & 1 deletion migrations/m250707_104009_multiple_tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ public function safeUp()
{
$rawPrimaryKey = 'NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY';

if ($this->db->driverName === 'mysql') {
$tableOptions = 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
}

$this->createTable(
'{{%multiple_tree}}',
[
'id' => $this->db->driverName !== 'oci' ? $this->primaryKey()->notNull() : $rawPrimaryKey,
'tree' => $this->integer()->null(),
'name' => $this->db->driverName === 'oci' ? $this->string()->notNull() : $this->text()->notNull(),
'name' => $this->string(255)->notNull(),
'lft' => $this->integer()->notNull(),
'rgt' => $this->integer()->notNull(),
'depth' => $this->integer()->notNull(),
],
$tableOptions,
);

$this->createIndex('idx_multiple_tree_tree', '{{%multiple_tree}}', 'tree');
Expand Down
Loading