/** * Updates an existing Post 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()) { $model->unlinkAll('categories', true); foreach (Category::getCategoriesById($model->categories_id) as $category) { $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } else { $model->categories_id = ArrayHelper::getColumn($model->categories, 'id'); return $this->render('update', ['model' => $model]); } }
/** * Updates an existing Post model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { /** @var Post $model */ $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { $model->unlinkAll('categories', true); foreach (Category::getCategoriesById($model->categories_id) as $category) { $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } else { /** @var Category $category */ foreach ($model->categories as $category) { if ($category->active) { $model->categories_id[] = $category->id; } } return $this->render('update', ['model' => $model]); } }