我有一个奇怪的问题
在AR模型中,Gift与类别有many_many关系
'categories'=>array(self::MANY_MANY, 'GiftCategory',
'tbl_category_gift(gift_id, category_id)'),
当我调用 $gift->categories 时,它返回事件记录对象,并且在数据库中我看到值已正确存储
但是当我写
<? echo $form->checkBoxList($model, "categories", GiftCategory::listData()); ?>
在我的复选框列表中,如果模型有类别,则仅选择 1 个值,我不明白 wtf 是不是,请帮助:)
问题:Yii activeCheckBoxList 仅显示 1 个选中项(仅第一个)
最佳答案
它不起作用,因为用Yii creator Qiang的话来说:
...in activeListBox(), the second parameter should be an attribute name, not a relation name. In your case, you may need to declare a new property to store the selections.
我相信同样的情况也适用于 activeCheckBoxList。
我解决这个问题的方法如下(这篇博文很有帮助 Retrieving selected checkbox items in yii )
向您的关系添加索引选项
'categories' => array(self::MANY_MANY,
'Category', 'post_category(post_id, category_id)','index'=>'id'),
向模型添加属性
public $selectedCategoryIds
在 afterFind 中填充此属性
public function afterFind()
{
parent::afterFind();
$this->selectedCategoryIds = array_keys($this->categories);
}
在您的 View 中使用这个新的“属性
<? echo $form->checkBoxList($model, "selectedCategoryIds", GiftCategory::listData()); ?>
为了正确处理更新此数据,请参阅 Larry Ullman 的文章 Handling Related Models in Yii Forms
关于php - Yii activeCheckBoxList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12796702/