【发布时间】:2015-04-14 02:45:08
【问题描述】:
我正在从基本安装修改User.php 模型。
这就是我现在拥有的:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return null;
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === \Yii::$app->getSecurity()->generatePasswordHash($password);
}
}
LoginForm.php 正在调用它:
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
我遇到的问题是,当程序到达User.php 的validatePassword 时,$this->password 总是null 并且验证失败。
我确信找到了记录,因为我可以看到 LoginForm.php's $this->_user 中的值。否则,当我试图找到一些随机用户时,它将是 null。
如何使用静态函数中找到的填充User.php's $this->password?
【问题讨论】: