0

I got an error message, when i want to load back saved elements on update form.

Controller

$contentCategory = ContentCategory::find()
->where(['content_id' => $id])->all();

View

<?= $form->field($contentCategory, 'category_id')
->dropdownList(ArrayHelper::map(Category::find()->all(),'id','title'),
    ['prompt'=>'Select Category', 'multiple' => 'multiple']
)->label('Add categories'); ?>

The error message.

Call to a member function isAttributeRequired() on array

If i change the all() method to one() it's works but select only one element (of course).

Update: @scaisEdge I'm using a content_category junction table to insert relations contents with categories.

content_id  category_id
1           2
1           3

Model

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']],
    ];
}

3 Answers3

0

The trouble in that you use $contentCategory array of founded models as form field model.

I think you should just change $contentCategory variable from your view on your certain model like $model = ContentCategory::findOne($id);

Dmitry
  • 242
  • 1
  • 9
  • 20
0

@Csaba Faragó

$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
],
]);  
?>

do all this in view page

shikha
  • 19
  • 1
  • 6
0

I have found a solution here. He is perfectly solve my issue.