extending CWebUser

Hi,

I followed the wiki to create my custom User model.

http://www.yiiframework.com/wiki/60/add-information-to-yii-app-user-by-extending-cwebuser/

I realised that whenever I need to get user attributes, if($this->_model===null) is always null, and hence it has to load from db everytime.




protected function loadUser($id=null)

    {

        if($this->_model===null)

        {

            if($id!==null)

                $this->_model=User::model()->findByPk($id);

        }

        return $this->_model;

    }

}



It shouldn’t be null after $this->_model=User::model()->findByPk($id); isn’t it?

it probably loads everytime bacause it doesnt find the user when it tries to load, therefore the result is null, and _model stays null

I thought after the first time of loading from db,

if($this->_model===null) will be false, as it will be saved in the session??

For my case, for whichever controllers that wants to get user attributes,




controller1

-----------

function aa()

{

Yii::app()->user->getName(); // will load from db

Yii::app()->user->getAddress(); // $this->_model is not null

}



whenever aa() is called, the first call will be null. Subsequent calls within the same function works fine.

halo…anyone enlighten me on me??

if it is supposed to load everytime… is there a way for me to really cache it so as to save the db query??




protected function loadUser($id=null)

    {

        if($this->_model===null)

        {

            if($id!==null){

                $this->_model=User::model()->findByPk($id);

                if($this->_model===null)

                     $this->_model=false;

            }

        }

        return $this->_model;

    }

}






 if($this->_model===null)

 $this->_model=false;



pardon me…how does this caches the model??

I believe what you need to use is set/getState




protected function loadUser($id=null)

    {

        if($this->_model===null)

        {

            if($id!==null){

                $this->_model=User::model()->findByPk($id);

                $this->setState('model', $this->_model);

            }

        }

        return $this->_model;

    }

}



Still no luck. I also try setting it after login.




Yii::app()->user->login($identity,$duration);

Yii::app()->user->setState('model', Yii::app()->user->getModel());



Could it be some config that I need to define? rule? I really got no clue…

I do the same job of you, but I never save the model in the state.

A model is full of properties that we don’t want to save, is better to save in the session only the data you need in form of array, and use the model only if you have to save something in the database.




 if($this->_model===null)

 $this->_model=false;

will save that the user does not exist, if he does’nt

its probably something to do with the ID of the user you are trying to load

I managed to get it to work. Just to share and hope someone can point out if there is any mistakes…




SiteController

--------------


actionIndex()

{

$this->login();

Yii::app()->user->setState('model',Yii::app()->user->getModel()); // this is where i set the state

}


login()

{

Yii::app()->user->login($identity,$duration);

}


MyWebUser

---------

private $_model;


login()

{

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

$this->populateUser($id);

}


populateUser()

{

$this->_model=Yii::app()->user->getState('model');

....

}