当我想在更新表单上加载保存的元素时,我收到了一条错误消息。
控制器
$contentCategory = ContentCategory::find()
->where(['content_id' => $id])->all();
查看
<?= $form->field($contentCategory, 'category_id')
->dropdownList(ArrayHelper::map(Category::find()->all(),'id','title'),
['prompt'=>'Select Category', 'multiple' => 'multiple']
)->label('Add categories'); ?>
错误消息。
Call to a member function isAttributeRequired() on array
如果我将all()方法更改为one(),它可以工作,但只选择一个元素(当然)。
更新 @scaisEdge我使用content_category联结表来插入关系内容和类别。
content_id category_id
1 2
1 3
模型
public function rules()
{
return [
[['content_id', 'category_id'], 'integer'],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
[['content_id'], 'exist', 'skipOnError' => true, 'targetClass' => Content::className(), 'targetAttribute' => ['content_id' => 'id']],
];
}
答案 0 :(得分:0)
您使用已创建模型的$ contentCategory数组作为表单字段模型的麻烦。
我认为您应该从您对某个模型的视图中更改$contentCategory
变量,例如$model = ContentCategory::findOne($id)
;
答案 1 :(得分:0)
@CsabaFaragó
$arr = array();
$colorarr=array();
foreach ($detmodel as $key => $detailm){
//$detailm;
$id = $detailm['productid'];
$sid = $detailm['sizeid'];
$sidex= explode(",",$sid);
$i = 0;
foreach($sidex as $size_id)
{
$val = $sidex[$i];
$val = (int)$val;
array_push($arr, $val);
$i++;
}
<?php $sizelist = ArrayHelper::map(Size::find()->all(),'id','size');
// $idd = '4';
?>
<?php
$detailm->sizeid = $arr;
// print_r($detailm->sizeid);
?>
<?= $form->field($detailm, 'sizeid')->widget(Select2::classname(), [
'data' => $sizelist,
'language' => 'de',
'options' => ['placeholder' => 'Select sizes ...','multiple' => true],
'pluginOptions' => [
'tags' => true,
'tokenSeparators' => [',', ' '],
'maximumInputLength' => 10
],
]);
?>
在视图页
中完成所有这些操作答案 2 :(得分:0)
我找到了解决方案here。他完全解决了我的问题。