Note: Please also take a look at the newer EMenu extension. 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, a jQuery plugin for creating menus.

You may add additional Columns if you wish, and you can rename the table however you like.
The 'parent' field contains the id of the direct parent, while 'sort' defines, where our row gets displayed in the menu.

This reads as: the 'parent' of a row belongs to the parent-column of the same table, while we can gather the childs of a row by a relation of ourself with the HAS_MANY relation. We always want our childs get ordered by the column 'sort's. You can add additional relations if you want.

To test our newly created model, we need to insert some random test data:

to print out all existing models in our Database. As you may suggest, this output is not very nice. Since we want to use
the CHtml module of yii 1.1, we need to format our Output in an array like this:

This is a recursive function (note how the function calls itself) that gathers all subchilds of an element that are available
in the Database. Of course, when we run this function on our root node, we get all non-orphan childs.

Attention: please be sure to not use this Widget inside the div id="mainmenu" of standard-generated webapps because there seems to be some css inconsistency between yii's default css and superfish's default css. Maybe this will be fixed sometime.

Note the bunch of effects and options (like drop-shadow) you can configure with superfish in extensions/vendors/CDropDownMenu.js

After this, we want our Users to be able to easily move Menu entries around. To achieve this, we will use a Drop-Down List,
in which we can choose the parent of our selected element. We write this code-snippet to views/Hierarchy/_form.php:

I hope my small tutorial was helpful for you. There are some points that can be made even better, for example someone could change the admin CGridView to be collapsable, and the elements could be moved around by drag & drop. In the next version of this Tutorial i will use the nestedset extension to achieve the Hierarchy Structure. Thank you for reading & trying, and
don't hesitate to ask me when you have Questions.