/** * Updates an existing Categories model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { if (\Yii::$app->user->isGuest) { return $this->goHome(); } $model = $this->findModel($id); $indicatorsAvailable = IndicatorNames::find()->select(['name', 'id'])->indexBy('id')->column(); $indicatorsPresent = $model->getIndicators()->select(['name', 'id'])->indexBy('id')->column(); foreach ($indicatorsPresent as $value) { if (($key = array_search($value, $indicatorsAvailable)) !== false) { unset($indicatorsAvailable[$key]); } } if ($model->load(Yii::$app->request->post()) && $model->save()) { CategoriesIndicators::deleteAll('id_categories = ' . $id); foreach (Yii::$app->request->post()['Categories']['indicators'] as $id_indicator) { $catInd = new CategoriesIndicators(); $catInd->id_indicator_names = $id_indicator; $catInd->id_categories = $model->id; $catInd->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'indicatorsAvailable' => $indicatorsAvailable, 'indicatorsPresent' => $indicatorsPresent]); } }