Difference between #7 and #12 of
Creating a database-driven hierarchical Structure combined with CMenu and superfish

Changes

Title unchanged

Creating a database-driven hierarchical Structure combined with CMenu and superfish

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

menu

Content changed

Note: Please also take a look at the newer [EMenu extension](http://www.yiiframework.com/extension/emenu "Title"). This works even better that
 
the deprecated CDropDownMenu!
 
 
In this tutorial we will create a hierarchical Structure using the traditional adjacency list model. Yii's ActiveRecord paradigm makes it very easy to implement this structure via a join on itself. After this, we will use the new CMenu from yii 1.1 and implement it in conjunction with [superfish](http://users.tpg.com.au/j_birch/plugins/superfish/ "Title"), a jQuery plugin for creating menus.

The [CDropDownMenu extension](http://www.yiiframework.com/extension/cdropdownmenu "Title") will help us to accomplish this task.
[...]
The 'parent' field contains the id of the direct parent, while 'sort' defines, where our row gets displayed in the menu.

After that, we generate the model and a C-R-U-D interface
:
 
 
~~~
 
$ php protected/yiic shell
 
Yii Interactive Tool v1.1 (based on Yii v1.1.0)
 
Please type 'help' for help. Type 'exit' to quit.
 
>> model Hierarchy
 
>> crud Hierarchy
 
~~~
 
 
When this commands run without an error
 using [Gii](http://www.yiiframework.com/doc/guide/1.1/en/topics.gii).
 
 
Once the model is ready
, we should add two relation rules to our new created modelit:
[...]
public function relations()
{
 return array(    'getparent' => array(self::BELONGS_TO, 'Hierarchy', 'parent'),   'childs' => array(self::HAS_MANY, 'Hierarchy', 'parent', 'order' => 'sort ASC'),   );
}
```
[...]
```php
$model = Hierarchy::model()->findByPk(7);
$parent = $model->
getparent;
echo $parent->title;
// returns 'First Entry'
[...]
$subitems[] = $child->getListed();
}
$returnarray = array('label' => $this->
headlintitle, 'url' => array('Hierarchy/view', 'id' => $this->id));
if($subitems != array())
$returnarray = array_merge($returnarray, array('items' => $subitems));
[...]
$items[] = $model->getListed(); // note that the [] is important, otherwise CMenu will crash.

$this->widget('
zii.widgetapplication.extensions.CDropDownMenu',array(
'items'=>$items,
));
```

to render the menu with the content of our database.
[...]
15 0
24 followers
Viewed: 64 120 times
Version: 1.1
Category: Tutorials
Tags: menu
Written by: thyseus
Last updated by: thyseus
Created on: Jan 16, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history