yii2 kartik multi select with relation table error "Call to a member function isAttributeRequired() on array"

253 Views Asked by At

I encountered above error while update scenario, am trying to save the multi selected values into relation table('classification_vs_metric') along with master tables(classifications,metrics) id's in create metrics. but when i click on edit button on already created record i encounter this error.

Metrics Master

id name type
3 Land ha,m2
4 Floors Nos

Classification Master

id industry sector subsector
1 Construction Commercial Casino
2 Construction Commercial Cinema
3 Construction Commercial Convention Center

classification_vs_metric slave/relation table

id metric_id classification_id
1 3 1
2 3 2
3 3 3
4 4 1
5 4 2

and am using following method to get the slave table values in actionUpdate in metrics controller

 public function getClassificationVsMetrics1()
{
    return $this->hasMany(Classificationvsmetric::className(), ['metric_id' => 'id'])->select(['classification_id']);
}

as

 public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $classificationIndustry = array();
    $releations = $model->getClassificationVsMetrics1()->asArray()->all();
        

    if ($model->load(Yii::$app->request->post())) {
        if($model->save()){
           
            $classifications = Yii::$app->request->post()["Classificationvsmetric"]['classification_id'];
           
                $classVsmetric = Classificationvsmetric::deleteAll(['metric_id'=>$model->id]);
                
                foreach ($classifications as $key => $value) {
                    $Classificationvsmetric = new Classificationvsmetric();
                        $Classificationvsmetric->classification_id =(int)$value;
                        $Classificationvsmetric->metric_id = $model->id;
                        $Classificationvsmetric->save(false);
                   }
      
            return $this->redirect(['view', 'id' => $model->id]);
        }
        return $this->redirect(['view', 'id' => $model->id]);
    }

    return $this->render('update', [
        'model' => $model,
        'Classificationvsmetric' => $releations

    ]);
}

and in _form.php

 $classificationIndustry =  ArrayHelper::map(\common\models\Classifications::find()->all(),'id',function($model,$default){
        return $model["industry"]." - ".$model["sector"] ." - ".$model['sub_sector'];
    });
 echo $form->field($Classificationvsmetric[0], 'classification_id')->widget(Select2::classname(), [
            'data' => $classificationIndustry,   // error showing in this line
            'value'=>(!$model->isNewRecord ? [$result] : ''),
            'language' => 'en',
            'options' => ['placeholder' => 'Select classification(s)','multiple' => true],
            'pluginOptions' => [
             'allowClear' => true, 
            ],
        ]);

this is working fine with create scenario, but getting error "Call to a member function isAttributeRequired() on array" in edit scenario. Can any body help me !!

1

There are 1 best solutions below

0
On

Finally after 2 days of head scratching i find solution myself. I have made few minor changes like below In metric master

 public function getClassificationVsMetrics1()
{
   return $this->hasMany(Classificationvsmetric::className(), ['metric_id' => 'id']);
}

In metric controller

 public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $releations = $model->getClassificationVsMetrics1()->all();
        
    if ($model->load(Yii::$app->request->post())) {
        if($model->save()){
           
            $classifications = Yii::$app->request->post()["Classificationvsmetric"]['classification_id'];
           
                $classVsmetric = Classificationvsmetric::deleteAll(['metric_id'=>$model->id]);
               
                foreach ($classifications as $key => $value) {
                    $Classificationvsmetric = new Classificationvsmetric();
                        $Classificationvsmetric->classification_id =(int)$value;
                        $Classificationvsmetric->metric_id = $model->id;
                        $Classificationvsmetric->save(false);
                   }
                 if($Classificationvsmetric == true){
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
        }
        return $this->redirect(['view', 'id' => $model->id]);
    }

    return $this->render('update', [
        'model' => $model,
        'Classificationvsmetric' => $releations

    ]);
}

and in view am managed to pass the values stored in the slave table as default value for select2 widget.

 <?php 
    $classificationIndustry =  ArrayHelper::map(\common\models\Classifications::find()->all(),'id',function($model,$default){
        return $model["industry"]." - ".$model["sector"] ." - ".$model['sub_sector'];
    });
    
    foreach ($Classificationvsmetric as $key => $value) {
        $selected[]  = $value['classification_id'];
    }

    echo $form->field($Classificationvsmetric[0], 'classification_id')->widget(Select2::classname(), [
        'data' => $classificationIndustry,
        'language' => 'en',
        'options' => [ 'value'=>(!$model->isNewRecord ? $selected : ''),'placeholder' => 'Select classification(s)','multiple' => true],
        'pluginOptions' => [
            // 'disabled' => $is_readonly,
            'allowClear' => true,
            // 'maximumSelectionLength' => 3,
        ],
    ]);
   ?>

i know this is not feasible solution, but right now this will save my butt.

preselected values in select2 widget in update scenario with many to many relation