Reputation: 85
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
Upvotes: 2
Views: 5371
Reputation: 3875
here is a good solution for your case:
check this steps:
after doing this steps just call this:
Yii::app()->user->id;
Yii::app()->user->name;
Yii::app()->user->email;
Upvotes: 4
Reputation: 913
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;
Upvotes: 3