AR模型没有按照预期工作 [ 2.0 版本 ]
表设计如下:
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
`authKey` varchar(50) NOT NULL DEFAULT '',
`accessToken` varchar(50) NOT NULL DEFAULT '',
`lock` int(11) NOT NULL DEFAULT '-1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8;
有两条记录如下:
100 admin admin test100key 100-token -1
101 demo demo test101key 101-token -1
对应的AR模型类如下:
class User extends ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
/**
* @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 self::find()->where(['accessToken'=>$token])->one();
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return self::find()->where(['username'=>$username])->one();
}
/**
* @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 === $password;
}
}
具体调用代码如下:
$username = 'demo';
$user = User::findByUsername($username);
print_r($user);
按照AR模型的定义,应该输出如下的结果的:
app\models\User Object
(
[id] => 100
[username] => admin
[password] => admin
[authKey] => test100key
[accessToken] => 100-token
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 100
[username] => admin
[password] => admin
[authKey] => test100key
[accessToken] => 100-token
[lock] => -1
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 100
[username] => admin
[password] => admin
[authKey] => test100key
[accessToken] => 100-token
[lock] => -1
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
但是数据结果却是:
app\models\User Object
(
[id] =>
[username] =>
[password] =>
[authKey] =>
[accessToken] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 100
[username] => admin
[password] => admin
[authKey] => test100key
[accessToken] => 100-token
[lock] => -1
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 100
[username] => admin
[password] => admin
[authKey] => test100key
[accessToken] => 100-token
[lock] => -1
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
这样使用$user->id显然不能访问该记录的字段,所谓AR模型也就没与意义了,此时如果我给$user->username赋新值,然后调用$user->save(),我的本意是修改一条记录的,但是结果却是新增了一条记录。这个问题有没有人察觉过,这究竟是yii2的bug还是有意为之,如果有意为之的话,这样做的道理何在?
最佳答案
其他 0 个回答
没有找到数据。
38573
注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱10
- 威望0
- 积分10