@@ -64,6 +64,112 @@ composer require yii2-extensions/nested-sets-behavior
64643 . ** Optimizes queries** using boundary values for efficient tree traversal.
65654 . ** Supports transactions** to ensure data integrity during complex operations.
6666
67+ ## Database setup
68+
69+ The package includes migrations to create the necessary database tables for nested sets functionality.
70+
71+ ### Available migrations
72+
73+ The package provides two migration files.
74+
75+ 1 . ** Single tree migration** (` m250707_103609_tree.php ` ) - for single tree per table.
76+ 2 . ** Multiple trees migration** (` m250707_104009_multiple_tree.php ` ) - for multiple independent trees.
77+
78+ ### Using package migrations
79+
80+ #### Method 1: Run migrations directly from extension (Recommended)
81+
82+ Configure your console application to include the extension migrations path.
83+
84+ ``` php
85+ <?php
86+
87+ declare(strict_types=1);
88+
89+ use yii\console\controllers\MigrateController;
90+
91+ // console/config/main.php
92+ return [
93+ 'controllerMap' => [
94+ 'migrate' => [
95+ 'class' => MigrateController::class,
96+ 'migrationPath' => [
97+ '@console/migrations', // Your app migrations
98+ '@vendor/yii2-extensions/nested-sets-behavior/migrations', // Extension migrations
99+ ],
100+ ],
101+ ],
102+ ];
103+ ```
104+
105+ Then run migrations normally.
106+
107+ ``` bash
108+ # View available migrations (including extension migrations)
109+ ./yii migrate/history
110+
111+ # Run all pending migrations
112+ ./yii migrate
113+
114+ # Or run specific migrations
115+ ./yii migrate/up m250707_103609_tree
116+ ./yii migrate/up m250707_104009_multiple_tree
117+ ```
118+
119+ #### Method 2: Using migration path parameter
120+
121+ Run migrations directly without configuration changes.
122+
123+ ``` bash
124+ # Run single tree migration
125+ ./yii migrate/up --migrationPath=@vendor/yii2-extensions/nested-sets-behavior/migrations
126+
127+ # Run specific migration from extension
128+ ./yii migrate/up m250707_103609_tree --migrationPath=@vendor/yii2-extensions/nested-sets-behavior/migrations
129+ ```
130+
131+ ### Migration details
132+
133+ #### Single tree migration (` m250707_103609_tree.php ` )
134+
135+ Creates a ` tree ` table with the following structure:
136+
137+ ``` sql
138+ CREATE TABLE `tree ` (
139+ ` id` int (11 ) NOT NULL AUTO_INCREMENT,
140+ ` name` text NOT NULL ,
141+ ` lft` int (11 ) NOT NULL ,
142+ ` rgt` int (11 ) NOT NULL ,
143+ ` depth` int (11 ) NOT NULL ,
144+ PRIMARY KEY (` id` ),
145+ KEY ` idx_tree_lft` (` lft` ),
146+ KEY ` idx_tree_rgt` (` rgt` ),
147+ KEY ` idx_tree_depth` (` depth` ),
148+ KEY ` idx_tree_lft_rgt` (` lft` ,` rgt` )
149+ );
150+ ```
151+
152+ #### Multiple tree migration (` m250707_104009_multiple_tree.php ` )
153+
154+ Creates a ` multiple_tree ` table with the following structure:
155+
156+ ``` sql
157+ CREATE TABLE `multiple_tree ` (
158+ ` id` int (11 ) NOT NULL AUTO_INCREMENT,
159+ ` tree` int (11 ) DEFAULT NULL ,
160+ ` name` text NOT NULL ,
161+ ` lft` int (11 ) NOT NULL ,
162+ ` rgt` int (11 ) NOT NULL ,
163+ ` depth` int (11 ) NOT NULL ,
164+ PRIMARY KEY (` id` ),
165+ KEY ` idx_multiple_tree_tree` (` tree` ),
166+ KEY ` idx_multiple_tree_lft` (` lft` ),
167+ KEY ` idx_multiple_tree_rgt` (` rgt` ),
168+ KEY ` idx_multiple_tree_depth` (` depth` ),
169+ KEY ` idx_multiple_tree_tree_lft_rgt` (` tree` ,` lft` ,` rgt` )
170+ );
171+ ```
172+
67173### Basic Configuration
68174
69175Add the behavior to your ActiveRecord model.
@@ -111,31 +217,6 @@ class Category extends ActiveRecord
111217}
112218```
113219
114- ### Database schema
115-
116- Create the required database fields.
117-
118- ``` sql
119- -- Single tree structure
120- CREATE TABLE category (
121- id INT PRIMARY KEY AUTO_INCREMENT,
122- name VARCHAR (255 ) NOT NULL ,
123- lft INT NOT NULL ,
124- rgt INT NOT NULL ,
125- depth INT NOT NULL
126- );
127-
128- -- Multiple trees structure
129- CREATE TABLE category (
130- id INT PRIMARY KEY AUTO_INCREMENT,
131- tree INT ,
132- name VARCHAR (255 ) NOT NULL ,
133- lft INT NOT NULL ,
134- rgt INT NOT NULL ,
135- depth INT NOT NULL
136- );
137- ```
138-
139220### Basic Usage
140221
141222#### Creating and building trees
0 commit comments