表设计如下:
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;
public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
public static function findIdentityByAccessToken($token, $type = null)
{
return self::find()->where(['accessToken'=>$token])->one();
}
public static function findByUsername($username)
{
return self::find()->where(['username'=>$username])->one();
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
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还是有意为之,如果有意为之的话,这样做的道理何在?
YII2 的AR底层已经重写了get()和set()方法 他取值的时候 get()方法是在attributes这个地方获取的 所以请不要在模型中定义数据库字段 如果不懂 用gii生成数据库模型 或者看看框架下载安装好的例子