I have added getId method in UserIdentity class to get user id instead of username but it doestn't fetch me userid. After adding this getId method, both of the following will result as empty

          1. Yii::app()->user->getId();
          2. Yii::app()->user->id;

I posted UserIdentity class here, pls help to solve this issue

   class UserIdentity extends CUserIdentity
     {
   private $_id;
    public function authenticate()
{       
    $users= User::model()->findByAttributes(array('name'=>$this->name));
    $password= User::model()->findByAttributes(array('password'=>$this->password));
        if($users===null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;                
        }
        else if($password===null) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }
        else {        
            $this->_id=$users->id;   
            $this->errorCode = self::ERROR_NONE;

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

}

And i tried to use CDbCriteria to get id from user model, it fetches usename column only, id column will come as empty

有帮助吗?

解决方案

here is a good solution for your case:

check this steps:

http://www.yiiframework.com/wiki/80/add-information-to-yii-app-user-by-extending-cwebuser-better-version/

after doing this steps just call this:

Yii::app()->user->id;
Yii::app()->user->name;
Yii::app()->user->email;

其他提示

you can use another method for set user id

else {        
        $this->_id=$users->id;
        $this->setState('user_id', $users->id);

        $this->errorCode = self::ERROR_NONE;

    }

and call like this

Yii::app()->user->user_id;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top