oaziz
oaziz

Reputation: 1372

Yii: BELONGS_TO & dropDownList

Let's say I have a tbl_article and tbl_category and I generated the CRUD for both. Now I need to have a drop down list for the category_id field in the article CRUD to show the category names instead of entering the category ID by hand. How can I do that?

I also have this set in my article model:

public function relations()
{
        return array(
                'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
        );
}

How can I change this correctly:

<div class="row">
        <?php echo $form->labelEx($model,'category_id'); ?>
        <?php echo $form->dropDownList($model,'category_id',???); ?>
        <?php //echo $form->textField($model,'category_id'); ?>
        <?php echo $form->error($model,'category_id'); ?>
</div>

Upvotes: 1

Views: 2325

Answers (1)

Alexander Shestakov
Alexander Shestakov

Reputation: 179

<?php 
$list = CHtml::listData(Category::model()->findAll(array('order' => 'name')), 'id', 'id'));
    echo $form->dropDownList($model,'category_id',$list); 
?>

Upvotes: 3

Related Questions