我不知道为什么 authenticate() 在这个简单的例子中不起作用:
这是我的用户表中的一条记录
模型/用户.php
class User extends CActiveRecord
{
public $id;
public $name;
public $lastname;
public $login;
public $password;
public $date;
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'user';
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Imię',
'lastname' => 'Nazwisko',
'login' => 'Login',
'password' => 'Hasło',
'date' => 'Data rejestracji',
);
}
public function rules()
{
return array(
array('name','required'),
array('lastname','required'),
array('login','required'),
array('password','required'),
array('date','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'insert')
);
}
}
模型/UserIdentity.php
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record = user::model()->findAllByAttributes(array('login' => $this->username));
if($record === null)
{
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
else if($record->password !== md5($this->password))
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else
{
$this->_id = $record->id;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
控制器/用户控制器.php
[...]
public function actionLogin()
{
$username = 'janek';
$password = '1234';
$identity=new UserIdentity($username,$password);
if($identity->authenticate())
{
echo $identity;
}
else
{
echo "NOT OK";
}
}
[...]
并且当请求操作登录时,总是显示不正常。我从 yii doc 修改示例。