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
  • seems a validation message ..update your question and add your model validation rules code .. Commented Aug 4, 2017 at 6:53
  • @Csaba, you are storing category_id as comma saperated ?? Commented Aug 4, 2017 at 7:28
  • pls show what you have passed to the view. Commented Aug 4, 2017 at 9:45

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.