### Get Whole Tree (Multiple Roots) with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Retrieves all nodes for a specific tree in a multiple-root setup by filtering on the 'root' attribute and ordering by 'lft'. ```PHP Category::model()->findAll(array('condition'=>'root=?','order'=>'lft'),array($root_id)); ``` -------------------------------- ### Adding Child Nodes with appendTo, insertAfter, prependTo (PHP) Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Shows another example of adding new Category nodes to a different root node. It uses `appendTo`, `insertAfter`, and `prependTo` to position the new nodes within the tree structure. ```php $category1=new Category; $category1->title='Samsung'; $category2=new Category; $category2->title='Motorola'; $category3=new Category; $category3->title='iPhone'; $root=Category::model()->findByPk(2); $category1->appendTo($root); $category2->insertAfter($category1); $category3->prependTo($root); ``` -------------------------------- ### Get Whole Tree (Single Root) with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Retrieves all nodes for a single-root tree by ordering them based on the 'lft' attribute, which is standard for the nested set model. ```PHP Category::model()->findAll(array('order'=>'lft')); ``` -------------------------------- ### Get All Root Nodes with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Retrieves all root nodes from the database using the roots() method provided by the NestedSetBehavior on a Yii 2 Active Record model. ```PHP $roots=Category::model()->roots()->findAll(); ``` -------------------------------- ### Get All Ancestors of a Node with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Finds a specific node by primary key and then retrieves all its ancestors using the ancestors() method provided by the NestedSetBehavior. ```PHP $category=Category::model()->findByPk(5); $ancestors=$category->ancestors()->findAll(); ``` -------------------------------- ### Get All Descendants of a Node with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Finds a specific node by primary key and then retrieves all its descendants using the descendants() method provided by the NestedSetBehavior. ```PHP $category=Category::model()->findByPk(1); $descendants=$category->descendants()->findAll(); ``` -------------------------------- ### Get All Children of a Node with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Finds a specific node by primary key and then retrieves its direct children using the children() method provided by the NestedSetBehavior. ```PHP $category=Category::model()->findByPk(1); $descendants=$category->children()->findAll(); ``` -------------------------------- ### Get Next Sibling of a Node with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Finds a specific node by primary key and then retrieves its next sibling using the next() method provided by the NestedSetBehavior. ```PHP $category=Category::model()->findByPk(9); $nextSibling=$category->next()->find(); ``` -------------------------------- ### Get Parent of a Node with Nested Set Behavior Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Finds a specific node by primary key and then retrieves its direct parent using the parent() method provided by the NestedSetBehavior. ```PHP $category=Category::model()->findByPk(9); $parent=$category->parent()->find(); ``` -------------------------------- ### Moving a Node to Become a New Root (PHP) Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Shows how to use the `moveAsRoot()` method to detach a node and its descendants from their current position and make the node the new root of a separate tree. ```php $node=Category::model()->findByPk(10); $node->moveAsRoot(); ``` -------------------------------- ### Adding Child Nodes with appendTo, insertAfter, insertBefore (PHP) Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Demonstrates adding new Category nodes as children of an existing root node using `appendTo`, `insertAfter`, and `insertBefore` methods provided by the nested set behavior. It shows how to create new model instances, set their attributes, find a parent node, and attach the new nodes relative to others. ```php $category1=new Category; $category1->title='Ford'; $category2=new Category; $category2->title='Mercedes'; $category3=new Category; $category3->title='Audi'; $root=Category::model()->findByPk(1); $category1->appendTo($root); $category2->insertAfter($category1); $category3->insertBefore($category1); ``` -------------------------------- ### Non-Recursive Tree Traversal (PHP) Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md Provides a method for traversing and displaying a nested set tree structure without using recursion. It fetches nodes ordered by `lft` (left boundary) and uses the `level` property to determine nesting depth and print HTML list tags (`