/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'index' page. * @return mixed */ public function actionCreate() { /** @var User $user */ $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'create']); $profile = \Yii::createObject(['class' => Profile::className()]); $r = \Yii::$app->request; $this->performAjaxValidation($user); if ($user->load($r->post()) && $user->create()) { $profile = $this->finder->findProfileById($user->id); $profile->load($r->post()); $profile->save(); Event::trigger(BaseController::class, BaseController::EVENT_MODEL_CREATED, new MessageEvent($profile)); $this->trigger(BaseController::EVENT_MODEL_CREATED, new MessageEvent($user)); \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been created')); return $this->redirect(['index']); } return $this->render('create', ['user' => $user, 'profile' => $profile]); }
/** @inheritdoc */ public function afterSave($insert, $changedAttributes) { if ($insert) { $profile = \Yii::createObject(['class' => Profile::className(), 'user_id' => $this->id, 'gravatar_email' => $this->email]); $profile->save(false); } else { if (\Yii::$app->user->getId() != $this->id && \Yii::$app->authManager->getIsAdmin(\Yii::$app->user->getId()) && $this->passwordChanged) { $this->mailer->sendPasswordChangeMessage($this); } } parent::afterSave($insert, $changedAttributes); }