public function actionUpdateProfile() { $model = UserProfile::findOne(Yii::$app->user->id); $model = $model ?: new UserProfile(['id' => Yii::$app->user->id]); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['profile']); } return $this->render('update-profile', ['model' => $model]); }
public function actionUpdateProfile() { $model = UserProfile::findOne(Yii::$app->user->id); $model = $model ?: new UserProfile(['id' => Yii::$app->user->id]); if ($model->load(Yii::$app->request->post())) { $transaction = Yii::$app->db->beginTransaction(); try { if ($model->save()) { $transaction->commit(); return $this->redirect(['profile']); } } catch (\Exception $exc) { $model->addError('', $exc->getMessage()); } $transaction->rollBack(); } return $this->render('update-profile', ['model' => $model]); }
/** * @return \yii\db\ActiveQuery */ public function getProfile() { return $this->hasOne(UserProfile::className(), ['id' => 'id']); }