/** * Updates an existing Exercise model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { if (Yii::$app->request->post()['Exercise']['test'] && isset(Yii::$app->request->post()['Exercise']['exerciseTests'])) { // echo json_encode(Yii::$app->request->post()['Exercise']['exerciseTests']); $new_id = \yii\helpers\ArrayHelper::getColumn(Yii::$app->request->post()['Exercise']['exerciseTests'], 'id'); $old_tests = \common\models\ExerciseTest::find()->where(['exercise_id' => $model->id])->andWhere(['not in', 'id', $new_id])->all(); foreach ($old_tests as $ot) { $ot->delete(); } foreach (Yii::$app->request->post()['Exercise']['exerciseTests'] as $exts) { if (\common\models\ExerciseTest::findOne($exts['id'])) { $modelET = \common\models\ExerciseTest::findOne($exts['id']); $modelET->value = $exts['value']; $modelET->istrue = $exts['istrue']; $modelET->save(); } else { $modelET = new \common\models\ExerciseTest(); $modelET->value = $exts['value']; $modelET->istrue = $exts['istrue']; $modelET->exercise_id = $model->id; $modelET->save(); } } } elseif (!isset(Yii::$app->request->post()['Exercise']['exerciseTests'])) { \common\models\ExerciseTest::deleteAll(['exercise_id' => $model->id]); } return $this->redirect(['control']); } else { return $this->render('update', ['model' => $model]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ExerciseTest::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'exercise_id' => $this->exercise_id, 'istrue' => $this->istrue]); $query->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }