### 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 (`
`, `- `).
```php
$criteria=new CDbCriteria;
$criteria->order='t.lft'; // or 't.root, t.lft' for multiple trees
$categories=Category::model()->findAll($criteria);
$level=0;
foreach($categories as $n=>$category)
{
if($category->level==$level)
echo CHtml::closeTag('li')."\n";
else if($category->level>$level)
echo CHtml::openTag('ul')."\n";
else
{
echo CHtml::closeTag('li')."\n";
for($i=$level-$category->level;$i;$i--)
{
echo CHtml::closeTag('ul')."\n";
echo CHtml::closeTag('li')."\n";
}
}
echo CHtml::openTag('li');
echo CHtml::encode($category->title);
$level=$category->level;
}
for($i=$level;$i;$i--)
{
echo CHtml::closeTag('li')."\n";
echo CHtml::closeTag('ul')."\n";
}
```
--------------------------------
### Modifying Tree Structure by Moving Nodes (PHP)
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md
Demonstrates how to rearrange nodes within the tree using various move methods like `moveAsFirst`, `moveBefore`, `moveAsLast`, and `moveAfter`. It shows how to find specific nodes by primary key and then reposition them relative to other nodes or within a parent.
```php
// move phones to the proper place
$x100=Category::model()->findByPk(10);
$c200=Category::model()->findByPk(9);
$samsung=Category::model()->findByPk(7);
$x100->moveAsFirst($samsung);
$c200->moveBefore($x100);
// now move all Samsung phones branch
$mobile_phones=Category::model()->findByPk(1);
$samsung->moveAsFirst($mobile_phones);
// move the rest of phone models
$iphone=Category::model()->findByPk(6);
$iphone->moveAsFirst($mobile_phones);
$motorola=Category::model()->findByPk(8);
$motorola->moveAfter($samsung);
// move car models to appropriate place
$cars=Category::model()->findByPk(2);
$audi=Category::model()->findByPk(3);
$ford=Category::model()->findByPk(4);
$mercedes=Category::model()->findByPk(5);
foreach(array($audi,$ford,$mercedes) as $category)
$category->moveAsLast($cars);
```
--------------------------------
### Identifying Node Type (isRoot, isLeaf, isDescendantOf) (PHP)
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md
Demonstrates how to use methods like `isRoot()`, `isLeaf()`, and `isDescendantOf()` to check the type and relationship of a node within the nested set tree structure. It uses `CVarDumper::dump` to output the boolean results.
```php
$root=Category::model()->findByPk(1);
CVarDumper::dump($root->isRoot()); //true;
CVarDumper::dump($root->isLeaf()); //false;
$node=Category::model()->findByPk(9);
CVarDumper::dump($node->isDescendantOf($root)); //true;
CVarDumper::dump($node->isRoot()); //false;
CVarDumper::dump($node->isLeaf()); //true;
$samsung=Category::model()->findByPk(7);
CVarDumper::dump($node->isDescendantOf($samsung)); //true;
```
--------------------------------
### Creating Root Nodes with NestedSetBehavior
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Create new root nodes by instantiating the model, setting attributes, and calling the `saveNode()` method. In single-tree mode, only one root can be created this way.
```php
$root=new Category;
$root->title='Mobile Phones';
$root->saveNode();
$root=new Category;
$root->title='Cars';
$root->saveNode();
```
--------------------------------
### Finding All Root Nodes with NestedSetBehavior
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Use the `roots()` method provided by the NestedSetBehavior to retrieve all root nodes in the tree(s). This returns an array of Active Record objects.
```php
$roots=Category::model()->roots()->findAll();
```
--------------------------------
### Upgrading NestedSetBehavior Node Access (v1.0.5)
Source: https://github.com/yiiext/nested-set-behavior/blob/master/upgrade.md
When upgrading from NestedSetBehavior v1.0.5, the methods for accessing parent, previous sibling, and next sibling nodes have changed. The old direct getter methods are replaced by chained calls involving relationship methods and a find() call.
```PHP
$parent=$node->getParent();
$prevSibling=$node->getPrevSibling();
$nextSibling=$node->getNextSibling();
```
```PHP
$parent=$node->parent()->find();
$prevSibling=$node->prev()->find();
$nextSibling=$node->next()->find();
```
--------------------------------
### Finding the Next Sibling of a Node
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Use the `next()` method on a specific node object to find its immediate next sibling node within the same parent. The `prev()` method can be used for the previous sibling.
```php
$category=Category::model()->findByPk(9);
$nextSibling=$category->next()->find();
```
--------------------------------
### Adding Child Nodes to a Specific Node (PHP)
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md
Illustrates adding new Category nodes as children to a non-root node (Audi). It uses `appendTo` and `prependTo` to add the nodes directly under the specified parent.
```php
$category1=new Category;
$category1->title='X100';
$category2=new Category;
$category2->title='C200';
$node=Category::model()->findByPk(3);
$category1->appendTo($node);
$category2->prependTo($node);
```
--------------------------------
### Create Root Nodes with Nested Set Behavior
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md
Creates new root nodes in the tree structure using the saveNode() method provided by the NestedSetBehavior. In single-tree mode, only one root can be created this way.
```PHP
$root=new Category;
$root->title='Mobile Phones';
$root->saveNode();
$root=new Category;
$root->title='Cars';
$root->saveNode();
```
--------------------------------
### Finding All Ancestors of a Node
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Use the `ancestors()` method on a specific node object to find all its ancestors, up to the root. This returns an array of Active Record objects.
```php
$category=Category::model()->findByPk(5);
$ancestors=$category->ancestors()->findAll();
```
--------------------------------
### Finding All Descendants of a Node
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Use the `descendants()` method on a specific node object to find all its descendants. This includes direct children, grandchildren, and so on.
```php
$category=Category::model()->findByPk(1);
$descendants=$category->descendants()->findAll();
```
--------------------------------
### Configure Yii 2 Nested Set Behavior
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme.md
Configures the NestedSetBehavior for a Yii 2 Active Record model by adding it to the behaviors() method. Specifies the database attributes used for left, right, and level values.
```PHP
public function behaviors()
{
return array(
'nestedSetBehavior'=>array(
'class'=>'ext.yiiext.behaviors.model.trees.NestedSetBehavior',
'leftAttribute'=>'lft',
'rightAttribute'=>'rgt',
'levelAttribute'=>'level',
),
);
}
```
--------------------------------
### Finding Entire Tree (Single Root) in Yii 2
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Retrieve all nodes of a single-root tree using standard Active Record methods, ordering by the 'lft' attribute to maintain tree structure.
```php
Category::model()->findAll(array('order'=>'lft'));
```
--------------------------------
### Finding Entire Tree (Many Roots) in Yii 2
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Retrieve all nodes for a specific root in a multi-root tree using standard Active Record methods, filtering by the root attribute and ordering by 'lft'.
```php
Category::model()->findAll(array('condition'=>'root=?','order'=>'lft'),array($root));
```
--------------------------------
### Finding Direct Children of a Node
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Use the `children()` method on a specific node object to find only its immediate direct children. This returns an array of Active Record objects.
```php
$category=Category::model()->findByPk(1);
$descendants=$category->children()->findAll();
```
--------------------------------
### Finding the Parent of a Node
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Use the `parent()` method on a specific node object to find its direct parent node. This returns a single Active Record object or null if the node is a root.
```php
$category=Category::model()->findByPk(9);
$parent=$category->parent()->find();
```
--------------------------------
### Configuring NestedSetBehavior in Yii 2 Model
Source: https://github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md
Configure the model's `behaviors()` method to attach the NestedSetBehavior. Specify the attributes used for left, right, and level columns. Avoid validation rules for these attributes.
```php
public function behaviors()
{
return array(
'nestedSetBehavior'=>array(
'class'=>'ext.yiiext.behaviors.model.trees.NestedSetBehavior',
'leftAttribute'=>'lft',
'rightAttribute'=>'rgt',
'levelAttribute'=>'level',
),
);
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.