Extending CWebUser and adding some properties to it


I’ve extended CWebUser in order to add ‘my’ user model to it, for accessing misc data in it that I need while rendering pages. I’ve consulted the following Wiki pages and my previous post on this issue is also given below:



my previous post: http://www.yiiframework.com/forum/index.php?/topic/20414-solved-design-for-login-and-persistent-user-information/

The relevant section of my custom CWebUser is:

class MyWebUser extends CWebUser {

    private $_model;

    public $firstName;

    public $lastName;


    public function login(IUserIdentity $identity, $duration=0) {

	parent::login($identity, $duration);

	// $identity->getUserModel() is my custom method that returns 'our' user object.

	$this->_model = $identity->getUserModel(); 

	$this->firstName = $this->_model->FirstName;

        // ...


Now for some reason Yii:app()->user->firstName isn’t stored across requests. I’m trying to pull it out in my main.php (layout file), just for debugging:

$u = Yii::app()->user;

echo $u->firstName;

Isn’t the Yii user object supposed to be fully persistent across requests? (I didn’t find approval or negation of this assumption in the class ref). I guess not… :slight_smile:

Also, I tried to use CWebUser::setState() to store the same data accompanied by commenting out the properties (public $firstName) and doing:

$this->setState('firstName', $this->_model->FirstName);

but that didn’t help as well.

Is there a way for me to store custom properties in Yii:app()->user so they are easily accessible via:



If its not possible, what would be the most clean and convenient methodology to achieve the same?



Hi there,

I truly do not know why with setState doesnt work. If you review the code of the following functions of CWebUser:






you will find the solution to your problem.

I would do the following:

a) extend from CWebUser

B) write an afterLogin event and save a state for the property you wish to use afterwards globally. It should be enough with the id to load the model as required, the model object from a method of UserIdentity is a bit out of place on that object isn’t it?

public function afterLogin(){

    if($this->_model === null && ($this->_model=User::model()->findByPk($this->id) )


    return true;


c) write a property to get what you wish to get.

public function getFirstName(){

    return $this->getState('__firstname');


Code totally untested but concept extracted from one of my latest applications.

Thanks again, Antonio :)

I wrote something here, then double checked myself and realized that getState() and setState() works fine. The problem is that I had to use getState(‘someProp’) and could not use Yii:app()->user->someProp directly. Must be related to my CustomerWebUser implementation style.

Anyhow works now, and I’m happy :slight_smile:

Thanks again,


Hi Boaz, can you write your extension of user in order to user firstname/lastname of user who is logging plz.

i don’t succes to it for now.

best regards.

shizo971 -> in order to what?.. . What exactly do you want me to show here?..