How to create and add nodes to the tree
masterTo build a tree, you can create new nodes and position them relative to existing ones.
Creating Root Nodes:
Use saveNode() on a new model instance.
Note: In single-tree mode, calling saveNode() when a root already exists will throw a CException.
Adding Child Nodes: Use the following methods to attach a new node to an existing one:
appendTo($node): Adds the node as the last child of$node.prependTo($node): Adds the node as the first child of$node.insertAfter($node): Adds the node immediately after$node(as a sibling).insertBefore($node): Adds the node immediately before$node(as a sibling).
Example:
$root = Category::model()->findByPk(1);
$newChild = new Category;
$newChild->title = 'New Category';
$newChild->appendTo($root);// Create root nodes
$root = new Category;
$root->title = 'Mobile Phones';
$root->saveNode();
// Add children
$category1 = new Category;
$category1->title = 'Ford';
$root = Category::model()->findByPk(1);
$category1->appendTo($root);