Question

Not sure if it's a best practice, but we wish to have a menu on a layout view and pass as URL parameter a value that should retrieve some data.

The menu would be something like:

<ul id="menu">
<li>districtA</li>
<li>districtB</li>
<li>districtC</li>
</ul>

So something like:

'url'=>array('/event/getEventsByDistrict', 'district'=>id),

Is there a better way to do it?

I mean, by doing like this, I believe, if a new district gets added to the database, then we should edit the menu by hand, placing the corresponding id there ? Isn't there a way to retrieve that id from the database, so that, every time the menu is rendered, we may be sure that that new item will link to the appropriate id of that new (just added district?) ?

Does this makes sense to you?

It seems that the only way could be that of creating a wrapper class or something ?

Perhaps districts are NOT a good example, they don't change often, but let's imagine something that changes often.

Please advice

Was it helpful?

Solution

If I understand your question correctly, this should work, assuming your District model has a name property.

$districts = DistrictModel::model()->findAll();
$menu = array();
foreach($districts as $district) $menu[] = array('label'=>$district->name, 'url'=>array('event/getEventsByDistrict', 'district'=>$district->id));

$this->widget('zii.widgets.CMenu', array(
    'items'=>$menu
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top