Php Yii-属于你的工作,现在抛出错误
我有一个用户和帐户模型。关系是用户属于帐户,帐户有多个用户 以下是两者的模型代码: 用户模型:Php Yii-属于你的工作,现在抛出错误,php,orm,yii,Php,Orm,Yii,我有一个用户和帐户模型。关系是用户属于帐户,帐户有多个用户 以下是两者的模型代码: 用户模型: public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'account'
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'account' => array(self::BELONGS_TO, 'Account', 'account_id'),
);
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::HAS_MANY, 'User', 'account_id'),
'userCount'=>array(self::STAT,'User','account_id'),
);
}
账户模式:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'account' => array(self::BELONGS_TO, 'Account', 'account_id'),
);
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::HAS_MANY, 'User', 'account_id'),
'userCount'=>array(self::STAT,'User','account_id'),
);
}
我在我的UserIdentity.php中有这段代码,用于登录,运行正常:
public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else{
if($user->password!==$user->encrypt($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else{
$this->_id=$user->id;
if($user->last_login_time==null)
$lastLogin=time();
else
$lastLogin=strtotime($user->last_login_time);
$this->setState('lastLoginTime', $lastLogin);
$this->setState('account',array('id'=>$user->account->id,'name'=>$user->account->name,));
$this->errorCode=self::ERROR_NONE;
}
}
return !$this->errorCode;
}
当我将另一个用户添加到帐户时,它开始出现错误:
PHP注意:尝试获取非对象的属性
错误指向
$this->setState('account',array('id'=>$user->account->id,'name'=>$user->account->name,)代码>
当分成多行时:
'id'=>$user->account->id,
是错误所在
为了解决这个问题,我简单地将其更改为:
$account=Account::model()->findByPk($user->account_id);
$this->setState('account',array('id'=>$account->id,'name'=>$account->name,));
因此,当我只有一个用户时,这种关系运行得很好,但当我有两个用户时,这种关系就失败了。如上所述,我可以继续使用Yii,但我确实喜欢直接访问对象的简单性。我没有正确地建立关系吗?为什么现在一个帐户中有两个用户无法使用此功能
编辑:
var\u dump($user)
-
另外有趣的是,我可以使用以下命令从帐户访问用户:$users=$account->users
并访问所有$user[0]
属性即可。因此,反过来说,关系似乎在起作用,只是向前看似乎有困难。不要在模型中声明与关系同名的变量
public $account;
将阻止模型查找帐户
关系,因为Yii将首先查找(并使用)实际属性,然后再检查同名关系。请在多行上拆分这一行,并将错误拆分为多行,以便更清楚哪个变量用作对象,而不是对象。您还应该var\u dump
候选错误,这样您就可以直接找到它是哪一个代码>以了解它是什么。我快速扫描了代码,我只能猜测它不是空的,也不是对象;)@hakre更新了。张贴在。一切看起来都是正确的。第663行是说明关系的地方。耸耸肩,这是很多输出:/-这不是有更好的调试工具吗?无论如何,我的错,试试var\u dump($user->account)代码>也是。我打赌这更有趣。你能删除第二个用户并验证它是否恢复正常工作吗?我发现很多时候,应该正常工作的事情是由我预期之外的事情引起的,所以这将有助于验证第二个用户是否确实是问题的原因。正如你所说,一切看起来都是正确的。