Yii Framework Forum: Problem To Get Loged In User Id After Login - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem To Get Loged In User Id After Login Rate Topic: -----

#1 User is offline   Mizanur 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-April 14
  • Location:Bangladesh

Posted 14 April 2014 - 12:01 PM

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.....
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,630
  • Joined: 04-March 10
  • Location:UK

Posted 14 April 2014 - 12:09 PM

CUserIdentity already provides that functionality, so you shouldn't need to override it. Yii::app()->user->id should provide the value you need.
0

#3 User is offline   Mizanur 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-April 14
  • Location:Bangladesh

Posted 14 April 2014 - 12:13 PM

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.

:( :(
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,697
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 April 2014 - 12:30 PM

Yii::app()->user->getId() gives you this:
http://www.yiiframew...er#getId-detail

So inspect your code, seems like you have overriden something.

Places to look:
UserIdentity (getId(), authenticate())
WebUser (if any)
LoginForm
...etc...
God is real unless declared as integer
0

#5 User is offline   Mizanur 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-April 14
  • Location:Bangladesh

Posted 14 April 2014 - 12:38 PM

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.
0

#6 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,697
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 April 2014 - 12:49 PM

You can override it in UserIdentity::authenticate()
$this->setState('id', $user->id);

God is real unless declared as integer
0

#7 User is offline   Mizanur 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-April 14
  • Location:Bangladesh

Posted 14 April 2014 - 12:54 PM

Thank you. Thanks a lot for you kindness. God bless you....

:) :) :)
0

#8 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 16 April 2014 - 09:03 PM

View PostKeith, on 14 April 2014 - 12:09 PM, said:

CUserIdentity already provides that functionality, so you shouldn't need to override it. Yii::app()->user->id should provide the value you need.

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;
}

Do, or do not. There is no 'try.' Jedi Master Yoda
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users