yii2 保存用户密码失败

Saving user password in yii2 fails

正在尝试更新用户密码,但无法保存

这是控制器代码:

public function actionChangepassword($id) {
    $model = new UserPass();
    $user = $this->findModel($id);

    if ($model->load(Yii::$app->request->post())) {
        if ($model->validate()) {
            try {
                $user->setPassword($_POST['UserPass']['newpass']);

                if (!$user->save()) {
                    Yii::$app->getSession()->setFlash('success', 'Password changed to ');
                    return $this->redirect(['indexi/'.$user->password]);
                } else {
                    Yii::$app->getSession()->setFlash('error', 'Password not changed');
                    return $this->redirect(['index']);
                }
            } catch (Exception $e) {
                Yii::$app->getSession()->setFlash('error', "{$e->getMessage()}");
                return $this->render('changepassword', [
                    'model' => $model
                ]);
            }
        } else {
            return $this->render('changepassword', [
                'model' => $model
            ]);
        }
    } else {
        return $this->render('changepassword', [
            'model' => $model
        ]);
    }
}

当我检查 var_dump($_POST['UserPass']['newpass']) 它有一个值;

当我尝试更改部分时 $user->setPassword($_POST['UserPass']['newpass']);

$modeluser->password = $_POST['UserPass']['newpass'];

密码已保存但未散列,

可能有什么问题

这是setpassword函数

public function setPassword($password)
{
    $this->password = Yii::$app->security->generatePasswordHash($password);
}

1) 改变

$user->setPassword($_POST['UserPass']['newpass']);

$user->newpass = $user->setPassword($_POST['UserPass']['newpass']);

2) 改变

public function setPassword($password)
{
    $this->password = Yii::$app->security->generatePasswordHash($password);
}

public function setPassword($password)
{
    return Yii::$app->security->generatePasswordHash($password);
}

最好的方法是看看它是如何在高级模板中实现的。