NestedSet Behavior

The nested_set behavior allows a model to become a tree structure, and provides numerous methods to traverse the tree in an efficient way.

Many applications need to store hierarchical data in the model. For instance, a forum stores a tree of messages for each discussion. A CMS sees sections and subsections as a navigation tree. In a business organization chart, each person is a leaf of the organization tree. Nested sets are the best way to store such hierarchical data in a relational database and manipulate it. The name "nested sets" describes the algorithm used to store the position of a model in the tree ; it is also known as "modified preorder tree traversal".

Basic Usage

In the schema.xml, use the <behavior> tag to add the nested_set behavior to a table:

Rebuild your model, insert the table creation sql again, and you're ready to go. The model now has the ability to be inserted into a tree structure, as follows:

<?php$s1=newSection();$s1->setTitle('Home');$s1->makeRoot();// make this node the root of the tree$s1->save();$s2=newSection();$s2->setTitle('World');$s2->insertAsFirstChildOf($s1);// insert the node in the tree$s2->save();$s3=newSection();$s3->setTitle('Europe');$s3->insertAsFirstChildOf($s2);// insert the node in the tree$s3->save();$s4=newSection();$s4->setTitle('Business');$s4->insertAsNextSiblingOf($s2);// insert the node in the tree$s4->save();/* The sections are now stored in the database as a tree: $s1:Home | \$s2:World $s4:Business |$s3:Europe*/

You can continue to insert new nodes as children or siblings of existing nodes, using any of the insertAsFirstChildOf(), insertAsLastChildOf(), insertAsPrevSiblingOf(), and insertAsNextSiblingOf() methods.

Once you have built a tree, you can traverse it using any of the numerous methods the nested_set behavior adds to the query and model objects. For instance:

Each of the traversal and inspection methods result in a single database query, whatever the position of the node in the tree. This is because the information about the node position in the tree is stored in three columns of the model, named tree_left, tree_right, and tree_level. The value given to these columns is determined by the nested set algorithm, and it makes read queries much more effective than trees using a simple parent_id foreign key.

Manipulating Nodes

You can move a node - and its subtree - across the tree using any of the moveToFirstChildOf(), moveToLastChildOf(), moveToPrevSiblingOf(), and moveToNextSiblingOf() methods. These operations are immediate and don't require that you save the model afterwards:

<?php// move the entire "World" section under "Business"$s2->moveToFirstChildOf($s4);/* The tree is modified as follows:$s1:Home |$s4:Business |$s2:World |$s3:Europe*/// now move the "Europe" section directly under root, after "Business"$s3->moveToNextSiblingOf($s4);/* The tree is modified as follows: $s1:Home | \$s4:Business $s3:Europe |$s2:World*/

If you delete() a node, all its descendants are deleted in cascade. To avoid accidental deletion of an entire tree, calling delete() on a root node throws an exception. Use the delete() Query method instead to delete an entire tree.

Filtering Results

The nested_set behavior adds numerous methods to the generated Query object. You can use these methods to build more complex queries. For instance, to get all the children of the root node ordered by title, build a Query as follows:

Multiple Trees

When you need to store several trees for a single model - for instance, several threads of posts in a forum - use a scope for each tree. This requires that you enable scope tree support in the behavior definition by setting the use_scope parameter to true:

Now, after rebuilding your model, you can have as many trees as required:

<?php$thread=ThreadQuery::create()->findPk(123);$firstPost=PostQuery::create()->findRoot($thread->getId());// first message of the discussion$discussion=PostQuery::create()->findTree(thread->getId());// all messages of the discussionPostQuery::create()->inTree($thread->getId())->delete();// delete an entire discussion$firstPostOfEveryDiscussion=PostQuery::create()->findRoots();

Using a RecursiveIterator

An alternative way to browse a tree structure extensively is to use a RecursiveIterator. The nested_set behavior provides an easy way to retrieve such an iterator from a node, and to parse the entire branch in a single iteration.

For instance, to display an entire tree structure, you can use the following code:

Parameters

By default, the behavior adds three columns to the model - four if you use the scope feature. You can use custom names for the nested sets columns. The following schema illustrates a complete customization of the behavior:

If your application used the old nested sets builder from Propel 1.4, you can enable the method_proxies parameter so that the behavior generates method proxies for the methods that used a different name (e.g. createRoot() for makeRoot(), retrieveFirstChild() for getFirstChild(), etc.