2011-12-21 18 views

回答

8

你應該的getId功能在用戶身份

+2

如上所述,您需要在您的身份類中實現getId()。它在這裏解釋:http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#defining-identity-class – 2011-12-21 09:28:57

9

的Yii ::應用程序() - >用戶默認返回組件CWebUser。

當你想獲得一些關於用戶的附加信息時,你需要擴展這個組件。

在您的components文件夾中創建文件WebUser.php。 (下面的示例所示)

class WebUser extends CWebUser { 
    /** 
    * Gets the FullName of user 
    * 
    * @return string 
    */ 
    public function getFullName() 
    { 
     return $this->_model->first_name . ' ' .$this->_model->last_name; 
    } 
} 

在您的配置文件中發現部分

'components'=>array(
'user'=>array(
'class'=>'WebUser' 
) 
) 

如果沒有這一部分,只是創建它。並將'class'=>更改爲WebUser'。

+0

爲什麼我們使用'components'=> array( ''user'=> array( 'class'=>'WebUser' ) )'?爲了添加額外的類? – Symfony 2011-12-21 06:17:00

+0

@Riaz用於擴展CWebUser行爲 – RusAlex 2011-12-21 08:54:59

+0

@RiazKhan:告訴Yii應該創建一個自己的'WebUser'類的實例(它提供了這個額外的功能),而不是內置的'CWebUser'。 – Jon 2011-12-21 14:13:52

1

使用的組件/的UserIdentity GETID功能得到用戶的ID和名稱等。

class UserIdentity extends CUserIdentity 
    { 

      private $_id; 
     public function authenticate() 
     { 
      $users=Users::model()->find("email_id='$this->username'");     

      $this->_id=$users->user_id; 

     } 

     public function getId(){ 
      return $this->_id; 

    } 
} 
2

或者你可以使用setState

class UserIdentity extends CUserIdentity 
{ 
    private $_id; 
    public function authenticate() 
    { 
     $record=Users::model()->findByAttributes(array('mail'=>$this->username)); 
     if($record===null) 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     else if($record->password!==md5($this->password."325")) 
      $this->errorCode=self::ERROR_PASSWORD_INVALID; 
     else 
     { 
      $this->_id = $record->id; 
      $this->setState('role', $record->active); 
      $this->setState('mail', $record->mail); 
      $this->setState('name', $record->name); 
      $this->setState('surname', $record->surname); 
      $this->setState('mobile', $record->mobile); 
      $this->setState('adress', $record->adress); 
      $record->count_login += 1; 
      $record->update(); 
      $this->errorCode=self::ERROR_NONE; 
     } 
     return !$this->errorCode; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 

} 


<?php echo Yii::app()->user->name." ".Yii::app()->user->surname; ?> 
0

,如果你想顯示它應該這樣做的意見:

echo Yii::$app->user->id; 
相關問題