/** * Finds the UserContact model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return UserContact the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id, $user_id = null) { if (($model = UserContact::findOne($id)) !== null) { return $model; } else { if ($user_id && ($model = UserContact::find()->where(['user_id' => $user_id])->one()) !== null) { return $model; } else { if (($model = User::findOne($user_id)) !== null) { $new_user_contact = new UserContact(); $new_user_contact->user_id = $user_id; return $new_user_contact; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } } }
/** * Updates an existing User 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); $contactModel = UserContact::findOne(['user_id' => $model->id]); /** @var UserContact $contactModel */ if ($model->load(Yii::$app->request->post()) && $contactModel->load(Yii::$app->request->post())) { if ($contactModel->save() && $model->save()) { UserTag::updateAll(['status' => 'inactive'], ['user_id' => $model->id]); $tags = Yii::$app->request->post('User')['tags']; array_walk($tags, function (&$arr) use($model) { $userTagModel = new UserTag(); $userTagModel->created_by = Yii::$app->user->id; $userTagModel->tag_id = $arr; $userTagModel->user_id = $model->id; if (!$userTagModel->save()) { throw new Exception("Cannot save user tag!"); } }); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'contactModel' => $contactModel]); } }