mizan3008
(Mizanur Rahman)
April 14, 2014, 5:01pm
1
Hi,
I’m very very beginner in Yii world. Somebody please tell how do I get a user id of logged in user I mean the primary key of logged in user.
I just use it in my UserIdentity.php class
private $id;
public function getId()
{
return $this->id;
}
I called it from layout/main.php file this way
echo Yii::app()->user->getId();
but it returned me controller id but I need logged in user table id.
Please help me…
Keith
(Kburton)
April 14, 2014, 5:09pm
2
CUserIdentity already provides that functionality, so you shouldn’t need to override it. Yii::app()->user->id should provide the value you need.
mizan3008
(Mizanur Rahman)
April 14, 2014, 5:13pm
3
Thank you for your help.
But I just removed my function getID() from UserIdentity.php and then I called echo Yii::app()->user->getId(); form layout/main.php but it returned me controller name not logged in user table id.
Yii::app()->user->getId() gives you this:
http://www.yiiframework.com/doc/api/1.1/CWebUser#getId-detail
So inspect your code, seems like you have overriden something.
Places to look:
UserIdentity (getId(), authenticate())
WebUser (if any)
LoginForm
…etc…
mizan3008
(Mizanur Rahman)
April 14, 2014, 5:38pm
5
Thank you I just fixed it but i just faced another problem this function print "Yii::app()->user->getId()" my user name but I need user row id.
You can override it in UserIdentity::authenticate()
$this->setState('id', $user->id);
mizan3008
(Mizanur Rahman)
April 14, 2014, 5:54pm
7
Thank you. Thanks a lot for you kindness. God bless you…
jkofsky
(Jkofsky)
April 17, 2014, 2:03am
8
No it doesn’t! The default implementation of of getId() is to return the username, same as getUsername().
To answer the OP’s question. You might need to change your variable name, $this->id maybe used by Yii for the name of the current object.
private $_id;
public function getId() {
return $this->_id;
}