0

我有 WebUser 的扩展版本。

它工作得很好。直到今天,我用时间激活了“自动登录”。现在,当用户回来并启动自动登录时,此方法出现错误:

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

这是否意味着自动登录不再创建“用户”属性?

如果我删除这些行,那么网站的其余部分到目前为止似乎工作正常。

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                                         
4

1 回答 1

3

当您调用 时Yii::app()->user->id,该user部分是CWebUser该类的一个实例(或该类的子类)。

When the application loads, it tries to initialize that user component, and as part of that initialization, it logs in a user, and then calls your overridden afterLogin method. Your afterLogin method in turn tries to call the component... but it hasn't finished initializing yet. So rather than get stuck in an infinite loop, Yii just tells you that the user component doesn't exist.

Fortunately, during the login process, a call to changeIdentity is made, which calls setId, which populates the $id attribute of the user component. This id attribute is in fact exactly the same attribute that is called with Yii::app()->user->id, only it is accessed within the object, instead of externally.

于 2013-01-17T16:01:57.723 回答