Skip to main content

Using CTreeView with NestedSetBehavior in Yii

Getting this whole thing to work took me a few days of experimenting. Finally I've got it working. Posting it here hoping that it would save others some time and if I need it back in the future.

The Yii nested set behavior extension can be found at

Download the zip file and then extract it into the "app root"/protected/extensions/NestedSetBehavior folder. Make sure that the NestedSetBehavior.php can be found at "app root"/protected/extensions/NestedSetBehavior/NestedSetBehavior.php.

Then modify the model file which you want to use the behavior with. For example, I have a model Areas and the file "app root"/protected/models/Areas.php. Add to the mentioned file the following lines:
 public function behaviors()
  return array(

The table for the model has to have the lft, rgt, level, and root_area field. All the field of type integer. The hasManyRoots determine whether the behavior should support more than 1 root per table, and so if it is true, the root_area will contain the root node id.

Then you need to modify the actionCreate and the actionUpdate method of the controller to use $model->saveNode(); rather than $model->save();

And that should be it to get nested behavior working. But.... how would you display it using the CTreeView widget? And how would you update it using that?

To display the tree, in the view, it is as simple as:
$this->widget('CTreeView', array(

But to get the $treedata? I've added the following 2 function into the model file.
public function treechild($id)
  $curnode = Areas::model()->findByPk($id);
   $childrens = $curnode->children()->findAll();
    $out = array();
    foreach($childrens as $children){
    return $out;
    return null;
  return null;

 public function treedata()
  $roots = Areas::model()->roots()->findAll();
  $out = array();
  foreach($roots as $root){
  return $out;

The treedata function would iterate over all the root nodes in the table and the treechild function would iterate over all the child nodes so that the data would be put into a properly formatted data structure. The CTreeView require an array with at least the id field for id, and text field to be displayed in the tree.

And so the controller is as simple as:

But that's only good for displaying the tree, to fully modify the tree (add new nodes and updating existing nodes), I need to add the following javascript to the view:
$('#treelink li').on('click', function(event) {

And the following menu (do note that I'm currently using the default layout which displays the menu widget):
 array('linkOptions'=>array('id'=>'create'),'label'=>'Create Areas', 'url'=>array('create')),
 array('linkOptions'=>array('id'=>'edit'),'label'=>'Edit Area', 'url'=>array('update')),
 array('label'=>'Manage Areas', 'url'=>array('admin')),

And in the create view:
echo $this->renderPartial('_form', array('model'=>$model,'parent_id'=>$parent_id)); 

And in the _form view:
 echo CHtml::hiddenField('parent_id',$parent_id);

And in the actionCreate method in the controller, added after the "$model->attributes=$_POST['Areas'];" line:
    $parent = Areas::model()->findByPk($_POST['parent_id']);

And the render line would be changed to:

So what the whole thing actually did, was to bind the li items in the node tree to change the create and edit link when clicked. The create link will be changed so that the currently selected node id set as parent_id and the edit link is changed so that id is the currently selected node id. Once the create method received the parent_id variable, it would be passed on all the way into the form. And once saved, if the parent_id actually exists and points to a real node, the new node would be set to append to the aforementioned node. There isn't much change for the update method.

And that folks, is how you would use the CTreeView with the NestedSetBehavior.


Anonymous said…
That extension is SOOOO poorly documented. The methods are all named to do things other than what they actually do. It's a shame because it's clearly a lot of hard work, but half-ass documentation makes it a real pig to work with.
Anonymous said…
Poorly documented?
You mean NestedSetBehaviour?

Actually it is pretty well documented.
ROFL - stop complain, start to read.

Thanks for your article.

Popular posts from this blog

Dell Inspiron Mini 9

Yesterday went to Low Yat to buy my cousin a sub RM 1500 notebook. Of course with that kind of price tag it would have to be a netbook jelah. Anyhow, I was short on time so I didn't get to browse thoroughly. Just went for one quick round around level 2 and picked the one that I thought would fulfil his need. The thing is, as soon as I stepped out of the lift there was a shop that sold the new Dell Inspiron Mini 9 for only RM 1199. I was suprised. Just 1199. I couldn't believe my eyes. With original Windows XP and all. RM 1199. I thought there must be something wrong. Better survey first. So went around level 2. There was nothing that could match that price. Most of the other netbook from acer, asus and lenovo even was around RM 1600. So I decided I'll just go with the Dell lah. But to tell you the truth if I had money to buy for my own use, I'd go with the Lenovo Ideapad S10. Fuh.. she's one sweet mama... Smooth rounded curves, slim white profile. Pergh.. But anyway

Food first post

My blogs' name is High-Tech Rojak but I don't recall ever talking about food. So here's a first. Just recently I got some free time and finally got to cook the pasta I've bought for ages. So here's an account of how it went down.. :) Okay. That's the thing I cooked. I have no idea what it's called. At first I was thinking of buying the ribbon ones, then my wife looked at the colorful spiral ones and said "why not get these? they're more colorful" and so we got them (yes, we know nothing about pasta.. :) So I boiled it, actually put some salt and oil into the water so that they won't stick, drained it and tadaaaaa, you'd get the above. I remember once I tried to cook macaroni and I didn't drain it after boiling it, it filled up the whole pot. LOL... Learned my lesson. Next up the sauce. Like I said we don't know anything about pasta so here's the ingredients we prepared. Yes ladies and gentleman. Instant pasta sauce all bot

First godot game published

I haven't published anything in google play for a very long time. I'm very happy to finally introduce my latest project to the public. It is a casual game called..... Add Block. Yeah.. The best original game name award coming right up. But seriously, try it out. Find in at the play store here . I love playing casual games on my phone. It's great to pass the time. But it doesn't really do anything for your skills. Or at least not anything for real life important skills (yes, I don't consider being able to identify the consecutive shapes or color as really important). So I decided to create one. So in this game, you practice your math skills. Find the adjacent blocks that add up to the total you need. So it's math, math is important right? Right?.... The levels are unlimited, the idea is that you keep on playing until you can't anymore. You'll die if you finally can't get the number of wins required to pass the level in time. You only have 100 se