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.
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.
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');
....
}