如何使用yii2(Advanced Template
)中的SignupForm.php来帮助用户更新数据?
我不想使用任何外部模块。
我有SignupForm.php模型:
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->first_name = $this->first_name;
$user->last_name = $this->last_name;
$user->sub_id = $this->sub_id;
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->status=10;
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
public function findModel($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
我有SiteController.php控制器:
public function actionProfile()
{
if (Yii::$app->user->isGuest) {
return $this->goHome();
}
$id = Yii::$app->user->id;
$model = SignupForm::findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->save();
return $this->redirect(['/event/index']);
} else {
return $this->redirect(['user/view/?id='.$id]);
}
}
public function actionSignup()
{
$this->layout = 'loginlayout';
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
答案 0 :(得分:1)
可能是你正在寻找的这个动作
Paste Values
并且对于密码,您需要像这样的函数
public function actionUpdate()
{
if (Yii::$app->user->isGuest) {
return $this->goHome();
}
对于passwrod,您不能简单地指定值,但必须使用类似setPassword和其他变量的函数更新值
$id = Yii::$app->user->id;
$model = SignupForm::findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->save();
return $this->redirect(['/event/index']);
} else {
return $this->render('signup', [
'model' => $model,
]);
}
}
}
根据需要调整字段名称