简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM