CHtml::listData
9. Juni 2010
Eine kleine hierarchische Struktur:
- section (id, name)
- category (id,sectionId, name)
- item (id, category, title)
Die activeDropDownList für das Feld categoryId im Item-Formular schreiben wir so:
<div class="simple">
<?php echo CHtml::activeLabelEx($model,'categoryId'); ?>
<?php echo CHtml::activeDropDownList($model,'categoryId', Item::getSectionCategoryOptions()); ?>
</div>Die Methode Item::getSectionCategoryOptions()
public static function getSectionCategoryOptions() { $sections = Section::model()->findAll(); $tmp = array(); foreach ($sections as $section) { $tmp[$section->name] = CHtml::listData($section->categories, 'id', 'name'); } return $tmp; }
ich frag mich, ob das auch einfacher geht.
Link zur Doku.
UPDATE:
<div class="simple">
<?php echo CHtml::activeLabelEx($model,'categoryId'); ?>
<?php echo CHtml::activeDropDownList($model,'categoryId', CHtml::listData(Category::model()->with('section')->findAll(), 'id', 'name', 'section.name')); ?>
</div>No related posts.
Letzte Kommentare