zii.widgets.CMenu and iterable object as items

I have object (CList) as items (nested items), but CMenu prohibits any use of Iterable object using only one expression:


	protected function normalizeItems($items,$route,&$active)

	{

		...

		...

191:		return array_values($items);

	}

Jonah, Qiang, please, fix this issue (using next expression etc.):


	protected function normalizeItems($items,$route,&$active)

	{

		if($items instanceof Traversable)

		    $items = iterator_to_array($items, true);

THANKS!

I’m a n00b still, but could this problem be related?

Model MenuItems.php




/**

 * Returns child items array (thanks to thyseus)

 */

public function findMenuItems($category)

{

	$subitems = array();

	if($this->relFromChildren)

	{

		foreach($this->relFromChildren as $child)

		{

			if ($child->menuitem_category == $category)

			{

				$subitems[] = $child->findMenuItems($category);

			}

		}

	}

	$menuarray = array(

		'label'=>$this->menuitem_label,

		'url'=>$this->menuitem_url,

		'visible'=>$this->menuitem_visible != NULL ? $this->menuitem_visible : false ,

		'template'=>$this->menuitem_template != NULL ? $this->menuitem_template : NULL,

		'linkOptions'=>$this->menuitem_linkoptions != NULL ? $this->menuitem_linkoptions : NULL,

		'itemOptions'=>$this->menuitem_itemoptions != NULL ? $this->menuitem_itemoptions : NULL,

	);

	if($subitems != array())

		$menuarray = array_merge($menuarray, array('items'=>$subitems));

		return $menuarray;

}



Controller SiteController.php




/**

 * This is the default 'index' action that is invoked

 * when an action is not explicitly requested by users.

 */

public function actionIndex()

{

	// load user menu items

	$modelMenuItems = MenuItems::model()->findByPk(1);

	$reqmenu[] = $modelMenuItems->findMenuItems('user');

	$menuitems = $reqmenu[0][items];

		// renders the view file 'protected/views/front/site/index.php'

	// using the default layout 'protected/views/front/layouts/main.php'

	$this->render('index', array(

		'items'=>$menuitems

	));

}



Layout main.php




<?php

	$this->widget('zii.widgets.CMenu',array(

		'items'=>$items,

        ));

?>



View index.php




<?php $this->pageTitle=Yii::app()->name; ?>


<h1>Muy pronto...</h1>




Error message:




PHP Error


Description


Invalid argument supplied for foreach()


Source File


/home/content/(whatever)/yii/framework/zii/widgets/CMenu.php(176)


00164:         }

00165:     }

00166: 

00167:     /**

00168:      * Normalizes the {@link items} property so that the 'active' state is properly identified for every menu item.

00169:      * @param array the items to be normalized.

00170:      * @param string the route of the current request.

00171:      * @param boolean whether there is an active child menu item.

00172:      * @return array the normalized menu items

00173:      */

00174:     protected function normalizeItems($items,$route,&$active)

00175:     {

00176:         foreach($items as $i=>$item)

00177:         {

00178:             if(isset($item['visible']) && !$item['visible'])

00179:             {

00180:                 unset($items[$i]);

00181:                 continue;

00182:             }

00183:             if($this->encodeLabel)

00184:                 $items[$i]['label']=CHtml::encode($item['label']);

00185:             $hasActiveChild=false;

00186:             if(isset($item['items']))

00187:             {

00188:                 $items[$i]['items']=$this->normalizeItems($item['items'],$route,$hasActiveChild);

Stack Trace


#0 /home/content/p/e/p/pepereyes/html/yii/framework/zii/widgets/CMenu.php(111): CMenu->normalizeItems()

#1 /home/content/p/e/p/pepereyes/html/yii/framework/web/CBaseController.php(148): CMenu->init()



The error happens on the same normalizeItems function. However, if I code the layout as this…




<?php

	$modelMenuItems = MenuItems::model()->findByPk(1);

	$reqmenu[] = $modelMenuItems->findMenuItems('user');

	$menuitems = $reqmenu[0][items];


	$this->widget('zii.widgets.CMenu',array(

		'items'=>$menuitems,

	));

?>



it works fine, but I’d like to have a skinny view/thin controller/fat model. What is the problem? Thanks for the help.

I found out that the layout is not getting the data array from the controller; I’m searching why…