PHP code example of besnovatyj / yii2-nested-sets

1. Go to this page and download the library: Download besnovatyj/yii2-nested-sets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

besnovatyj / yii2-nested-sets example snippets


use Besnovatyj\NestedSets\NestedSetsColumnMap;
use Besnovatyj\NestedSets\NestedSetsRepository;
use Besnovatyj\NestedSets\NestedSetsService;

$columns = new NestedSetsColumnMap(
    leftAttribute: 'lft',
    rightAttribute: 'rgt',
    depthAttribute: 'depth',
    treeAttribute: 'tree',        // null — режим одного дерева
    sortAttribute: 'sort_order',  // null — корни без отдельной сортировки
);

$repository = new NestedSetsRepository(Menu::class, $columns, Yii::$app->db);
$service = new NestedSetsService($repository);

$service->createRoot($menu);                  // новый корень (вернёт id)
$service->appendTo($parent, $item);           // последним ребёнком
$service->moveBefore($node, $target);         // перед узлом
$service->makeRoot($node);                    // поддерево -> новый корень
$service->moveRootBefore($rootA, $rootB);     // сортировка корней (sort_order)
$service->deleteWithChildren($node);          // удалить с поддеревом
$service->deletePromotingChildren($node);     // удалить, подняв детей к родителю