2016-09-08 10:56:38 2100次浏览 1条回答 0 悬赏 10 金钱

表设计如下:

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还是有意为之,如果有意为之的话,这样做的道理何在?

最佳答案

  • sun1992 发布于 2016-09-08 13:56 举报

    YII2 的AR底层已经重写了get()和set()方法 他取值的时候 get()方法是在attributes这个地方获取的 所以请不要在模型中定义数据库字段 如果不懂 用gii生成数据库模型 或者看看框架下载安装好的例子

    1 条回复
    回复于 2016-09-08 16:16 回复

    受教了,五个字

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
38573
等待激活

38573

注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
  • 粉丝0
  • 金钱10
  • 威望0
  • 积分10

热门问题