Home > Yii > CHtml::listData

CHtml::listData

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.

Author: Categories: Yii Tags:
  1. Bisher keine Kommentare
  1. Bisher keine Trackbacks