2013-01-16 73 views
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           
+0

什麼是完整跟蹤堆棧? –

+0

我想如果你是一個客人Yii :: app() - >用戶不能存在。如果它在裏面「後登錄」,應該不是我(),我應該不是我(#)(#) – sensorario

+0

@Stefano yii/yiilite.php(3943):CWebUser-> restoreFromCookie(),那麼yii/yiilite.php(4087):WebUser-> afterLogin(true) –

回答

3

當你調用Yii::app()->user->id,該user部分是CWebUser類(或類的子類)的一個實例。

當應用程序加載時,它會嘗試初始化該組件,並且作爲初始化的一部分,它會登錄一個用戶,然後調用您的覆蓋afterLogin方法。您的afterLogin方法依次嘗試調用組件...但尚未完成初始化。因此,Yii不會陷入無限循環,而只是告訴你user組件不存在。

幸運的是,在登錄過程中,到changeIdentity進行呼叫時,它調用setId,其填充user組件的$id屬性。這個id屬性實際上與Yii::app()->user->id調用的屬性完全相同,只是它在對象內而不是外部訪問。