2011-12-21 13 views

Odpowiedz

8

powinien mieć funkcję getId w tożsamość użytkownika

+2

Jak wspomniano powyżej, musisz zaimplementować metodę getId() w klasie tożsamości. Wyjaśniono tutaj: http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#defining-identity-class –

9

Yii :: app() -> user zwraca CWebUser komponentu domyślnie.

Jeśli chcesz uzyskać dodatkowe informacje o użytkowniku, musisz rozszerzyć ten komponent.

Utwórz plik WebUser.php w swoim folderze components. (Mój przykład poniżej)

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

W sekcji znaleźć pliku config

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

jeśli nie ma tej sekcji, po prostu utwórz go. I zmień "class" => na WebUser '.

+0

dlaczego używamy "components" => array ( ''user' => array ( "class" => 'WebUser' ) ) '? Aby dodać dodatkowe zajęcia? – Symfony

+0

@Riaz dla rozszerzania zachowań CWebUser – RusAlex

+0

@RiazKhan: Aby powiedzieć Yii, że powinien utworzyć instancję twojej własnej klasy "WebUser' (która zapewnia tę dodatkową funkcjonalność) zamiast wbudowanego' CWebUser'. – Jon

1

użyć funkcji getId w części/Useridentity uzyskać identyfikator użytkownika i nazwę etc ..

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

Albo jesteś mogą używać 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

jeśli chcesz wyświetl go na widokach, co powinno zrobić:

echo Yii::$app->user->id;