我有一个扩展版本的WebUser。
它工作得非常好。直到今天,我用一段时间激活了“autoLogin”。现在,当用户回来并自动登录时,此方法出错:
public function afterLogin($fromCookie)
{
parent::afterLogin($fromCookie);
// Update Last Login
$model = $this->loadUser(Yii::app()->user->id);
$model->last_visit = new CDbExpression('NOW()');
$model->save();
}
错误是:
Undefined property: CWebApplication::$user
这是否意味着自动登录不再创建'user'属性?
如果删除这些行,到目前为止,该网站的其余部分似乎工作正常。
PHP notice
Undefined property: CWebApplication::$user
/Users/nathan/Git/web/protected/components/WebUser.php(16)
04 // protected/components/WebUser.php
05
06 class WebUser extends RWebUser {
07
08 // Store model to not repeat query.
09 private $_model;
10
11 public function afterLogin($fromCookie)
12 {
13 parent::afterLogin($fromCookie);
14
15 // Update Last Login
16 $model = $this->loadUser(Yii::app()->user->id);
17 $model->last_visit = new CDbExpression('NOW()');
18 $model->save();
19 }
20
21 // Return first name.
22 // access it by Yii::app()->user->first_name
23 function getDisplayName(){
24 $user = $this->loadUser(Yii::app()->user->id);
25 return $user->displayName;
26 }
27
28 function getNotificationCount(){
Stack Trace
#0
– /Users/nathan/Git/web/yii/yiilite.php(4087): WebUser->afterLogin(true)
4082 if($this->autoRenewCookie)
4083 {
4084 $cookie->expire=time()+$duration;
4085 $request->getCookies()->add($cookie->name,$cookie);
4086 }
4087 $this->afterLogin(true);
4088 }
4089 }
4090 }
4091 }
4092 protected function renewCookie()
#1
– /Users/nathan/Git/web/yii/yiilite.php(3943): CWebUser->restoreFromCookie()
3938 public function init()
3939 {
3940 parent::init();
3941 Yii::app()->getSession()->open();
3942 if($this->getIsGuest() && $this->allowAutoLogin)
3943 $this->restoreFromCookie();
3944 elseif($this->autoRenewCookie && $this->allowAutoLogin)
3945 $this->renewCookie();
3946 if($this->autoUpdateFlash)
3947 $this->updateFlash();
3948 $this->updateAuthStatus();
#2
– /Users/nathan/Git/web/yii/yiilite.php(1050): CWebUser->init()
1045 $config=$this->_componentConfig[$id];
1046 if(!isset($config['enabled']) || $config['enabled'])
1047 {
1048 unset($config['enabled']);
1049 $component=Yii::createComponent($config);
1050 $component->init();
1051 return $this->_components[$id]=$component;
1052 }
1053 }
1054 }
1055 public function setComponent($id,$component,$merge=true)
#3
– /Users/nathan/Git/web/yii/yiilite.php(905): CModule->getComponent("user")
900 $this->init();
901 }
902 public function __get($name)
903 {
904 if($this->hasComponent($name))
905 return $this->getComponent($name);
906 else
907 return parent::__get($name);
908 }
909 public function __isset($name)
910 {
#4
– /Users/nathan/Git/web/protected/controllers/SiteController.php(30): CModule->__get("user")
25 * This is the default 'index' action that is invoked
26 * when an action is not explicitly requested by users.
27 */
28 public function actionIndex()
29 {
30 if(Yii::app()->user->checkAccessOrDeny('readHome')){
31 //URLs to show in the list
32 $criteria = Yii::app()->params['currentCommunity']->getUrls();
33 $criteria->limit = 10;
34 $urls = Url::model()->findAll($criteria);
35
答案 0 :(得分:3)
当您致电Yii::app()->user->id
时,user
部分是CWebUser
班级的实例(或该班级的孩子)。
当应用程序加载时,它会尝试初始化该user
组件,并且作为初始化的一部分,它会在用户中登录,然后调用被覆盖的afterLogin
方法。您的afterLogin
方法依次尝试调用组件......但尚未完成初始化。因此,Yii只是告诉你user
组件不存在而不是陷入无限循环。
幸运的是,在登录过程中,会调用changeIdentity
,调用setId
,其中会填充$id
组件的user
属性。这个id
属性实际上与Yii::app()->user->id
调用的属性完全相同,只是在对象内访问,而不是在外部访问。