php - Yii2 multiple models loop save error -


i've table in have save multiple data, in controller i've implemented action:

public function actionupdateorder($id){     /*da testare*/     //$result = 0;     $result = true;     $s = new session;     $model = new slidersimages();     if ($new_order = yii::$app->request->post('order')) {         //$s['us_model'] = 0;         foreach ($new_order $key => $value) {             if ($model::find()->where(['slider_id' => $id, 'image_id' => $key])->all()) {                  $s['image_'.$key] = $model;                  $model->display_order = $value;                 //$result = ($t = $model->update()) ? $result + $t : $result;                 $result = $model->save() && $result;             }         }     }      return $result; } 

the data received right not result, thing action add new table row slider_id , image_id equal null, why model doesn't save correctly?

thanks

the thing when call

$model::find()->where(['slider_id' => $id, 'image_id' => $key])->all()

you don't change $model object itself. calling:

slidersimages::find()->where(['slider_id' => $id, 'image_id' => $key])->all()

so, later when call $model->save() saving $model object empty attributes (you changed display_order)

my advise here: try assign result of ->all() call new var , work it:

public function actionupdateorder($id){     /*da testare*/     //$result = 0;     $result = true;     $s = new session;     if ($new_order = yii::$app->request->post('order')) {         //$s['us_model'] = 0;         foreach ($new_order $key => $value) {             $models = sliderimages::find()->where(['slider_id' => $id, 'image_id' => $key])->all();             if (count($models)) {                 // loop through $models , update them             }         }     }      return $result; 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -