简体   繁体   English

Yii: BELONGS_TO & dropDownList

[英]Yii: BELONGS_TO & dropDownList

Let's say I have a tbl_article and tbl_category and I generated the CRUD for both.假设我有一个 tbl_article 和 tbl_category 并且我为两者生成了 CRUD。 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.现在我需要在文章 CRUD 中为 category_id 字段创建一个下拉列表来显示类别名称,而不是手动输入类别 ID。 How can I do that?我怎样才能做到这一点?

I also have this set in my article model:我的文章 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); 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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