Yii Framework Forum: Add more info to CWebUser - Yii Framework Forum

Jump to content

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

Add more info to CWebUser Deals about the recommended way to add info th CWebUser Rate Topic: -----

#1 User is offline   Arvind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 03-October 09
  • Location:Calgary, AB, Canada

Posted 08 February 2010 - 07:08 PM

Hello,

what is the recommended way to add more info to CWebuser.

1. Extend the CWebuser class ? and using the methods/getters-setters assign values like firsname/lastname.

OR

2. make entries like this inside the authenticate() method of UserIdentity ?
$this->setState('FirstName', $users->FIRSTNAME);
$this->setState('LastName', $users->LASTNAME);


thanks
Arvind
0

#2 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 08 February 2010 - 08:10 PM

WebUser extends CWebUser
{
// do it you like
}
0

#3 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 08 February 2010 - 11:20 PM

View PostArvind, on 08 February 2010 - 07:08 PM, said:

Hello,

what is the recommended way to add more info to CWebuser.

1. Extend the CWebuser class ? and using the methods/getters-setters assign values like firsname/lastname.

OR

2. make entries like this inside the authenticate() method of UserIdentity ?
$this->setState('FirstName', $users->FIRSTNAME);
$this->setState('LastName', $users->LASTNAME);


thanks
Arvind



I recommend you load the user model in your "WebUser" class like

class WebUser extends CWebUser {
public function getModel()
{
$user = User::model()->findByPk($this->getId());// providing the value returned from getId() is the PK in your user table/model
return $user;
}
}

you can then access the model like $user = Yii::app()->user->getModel(); for the current user.

if($user!==null)
{
// can access all public user AR methods and attributes eg: $user->firstname, $user->status... etc
}
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#4 User is offline   Arvind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 03-October 09
  • Location:Calgary, AB, Canada

Posted 09 February 2010 - 03:35 AM

Hello,

I might sound very dumb, but how come this works ?

I extend the CWebUser class like this
class WebUser extends CWebUser
{
public function Foo();
}

then how come Foo() is available to Yii::app()->user.
Is not Yii::app()->user of type CWebUser ?

We have extended CWebUser, so Foo() should be available to WebUser. So how come its available to Yii::app()->user ?
0

#5 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 09 February 2010 - 04:25 AM

View PostArvind, on 09 February 2010 - 03:35 AM, said:

Hello,

I might sound very dumb, but how come this works ?

I extend the CWebUser class like this
class WebUser extends CWebUser
{
public function Foo();
}

then how come Foo() is available to Yii::app()->user.
Is not Yii::app()->user of type CWebUser ?

We have extended CWebUser, so Foo() should be available to WebUser. So how come its available to Yii::app()->user ?



didn't you specify the class for the user component in your config to be 'WebUser'? if you did, then Yii::app()->user will reyurn an instance of WebUser.
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#6 User is offline   Arvind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 03-October 09
  • Location:Calgary, AB, Canada

Posted 09 February 2010 - 04:44 AM

aaah yes, now its all clear!!!
how did i miss that one ?

thanks a ton
Arvind
0

#7 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 09 February 2010 - 07:39 AM

http://www.yiiframew...oc/cookbook/60/
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
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