类UserIdentity不返回Yii中的属性

i made this code with a tutorial:

class UserIdentity extends CUserIdentity
{
    private $_id;
    private $_email;

    public function authenticate()
    {
        $user=User::model()->findByAttributes(array('username'=>$this->username));
        if($user===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($user->password!==md5($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$user->id;
            $this->_email=$user->email;
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

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

And i try to save that ID of the user, but it does not return the ID, it returns the emailadres, and the email is not defined when calling it like:

echo Yii::app()->user->email;

Please help me! Im new to Yii!

You can override two methods of CUserIdentity:

  1. getName() //to get user name
  2. getId() //to get user id

So you would have two properties by default:

private $_name;
private $_id;

And you need to override mentioned methods:

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

public function getName() {
    return $this->_name;
}

And

$this->_id=$user->id;
$this->name=$user->username;

If you want to to save another user info such as email, you can use setState(), for example:

$this->_id=$user->id;
$this->name=$user->username;
$this->setState('email', $user->email);

Then use it in your project like below:

Yii::app()->user->getState('email'); //will return stored email in session

To get id , name you can use:

$id=Yii::app()->user->id;
$name=Yii::app()->user->name;

In your case, it seems, yii is overriding name with email.