Yii 1.1: Generate a multi-lever array sub-category for a menu


Everyone have seen a multilayer menu in several CMS

How to do that using a table ? Lets start!

your model should have id, parent_id and title, where parent_id refers to another record of the same table

Also you should create an extra relation

public function relations() {
        return array(
            'Childs' => array(self::HAS_MANY, 'yourModel', 'parent_id'),

Now, in your controller (or component) create this methods

private static $menuTree = array();
   public static function getMenuTree() {
        if (empty(self::$menuTree)) {
            $rows = YourModel::model()->findAll('parent_id IS NULL');
            foreach ($rows as $item) {
                self::$menuTree[] = self::getMenuItems($item);
        return self::$menuTree;
    private static function getMenuItems($modelRow) {
        if (!$modelRow)
        if (isset($modelRow->Childs)) {
            $chump = self::getMenuItems($modelRow->Childs);
            if ($chump != null)
                $res = array('label' => $modelRow->title, 'items' => $chump, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
                $res = array('label' => $modelRow->title, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
            return $res;
        } else {
            if (is_array($modelRow)) {
                $arr = array();
                foreach ($modelRow as $leaves) {
                    $arr[] = self::getMenuItems($leaves);
                return $arr;
            } else {
                return array('label' => ($modelRow->title), 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));

And in your theme call the first method

$this->widget('zii.widgets.CMenu', array(
              'items' =>$this->getMenuTree()

Now you have make a multi-layer menu

Total 8 comments

#18225 report it
scieri at 2014/09/30 07:34am
Customize look

How can this be customized to use Bootstrap classes, hard-coded into the controller or at the view?

#15117 report it
Kostas Apazidis (KonApaz) at 2013/10/08 03:34pm
RE: CSS for multi lever menu

Hi blaces

If you want to set css for each level you could write css like that

ul li {
background: #f00;
ul li ul li {
background: #0f0;
ul li ul li ul li {
background: #00f;
#15113 report it
blaces at 2013/10/08 12:23pm
What kind of CSS do I use for the CMenu?


Are there good CSS solution for CMenu to view the multi level menu items? :)

#14707 report it
Kostas Apazidis (KonApaz) at 2013/09/04 05:40pm
RE: Left Menu Bar


as the left menu is the same with main menu (there are different css) yes it is possible! just add class zii.widgets.CMenu and manipulate the alignments by css

$this->widget('zii.widgets.CMenu', array(
              'items' =>$this->getMenuTree()
#14706 report it
Muhammad Shahzad at 2013/09/04 03:21pm
Left Menu Bar


I need to show left menu bar with categories and sub categories on home page, Is it possible?

#13861 report it
Kostas Apazidis (KonApaz) at 2013/07/02 04:38pm
About structure table
CREATE TABLE `category` (
  `parent_id` int(11) DEFAULT NULL,
  `title` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`);
#13853 report it
Kostas Apazidis (KonApaz) at 2013/07/02 04:48am
About permissions

Hi mahdi1986

Thank you for your comment

You could use Yii::app()->user->checkAccess('whattask') foreach

('label' => ($modelRow->title), 
'visible'=> Yii::app()->user->checkAccess('the_task'.$modelRow->id)
'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)))

but you have to know about the roles permissions, check that http://www.yiiframework.com/doc/guide/1.1/en/topics.auth and http://www.yiiframework.com/wiki/328/simple-rbac/

best regards

#13852 report it
mahdi1986 at 2013/07/02 03:38am

Hi, Thanks for this wiki

Can you tell us structure table in database and how can I check user have permission to item Menu with ACL??

for example: at below code I can check only admin user can access see item Users


sorry for bad english :D

Leave a comment

Please to leave your comment.

Write new article