Sessions - setState vs extend CWebUser

Dear All ,

Could some one check this case ? is it a bug or am I doing something wrong?

Here is the below simple code


class MyWebUser extends CWebUser {


    private $_dbUser = false;


     public function getDbUser()

        {

                if($this->_dbUser === false){

                        $this->_dbUser = $this->isGuest ? null : User::model()->findByPk($this->id);

                }

                return $this->_dbUser;

        }

 

}

Whenever I call Yii::app()->user->dbUser from different actions


 $this->_dbUser === false

is getting satisfied every time and pulling the data from the database . My assumption was It should call the


User::model()->findByPk($this->id)

very first time and for all other times it should be from session . I did put the trace statement inside the if condition and trace is printing for each call . Did some one checked this or noticed?

If I use


 Yii::app()->user->setState('dbUser', User::model()->findByPk($this->id)); 

The value is getting stored in session and I am able to use it for my further actions

I see some thing fishy going on with the first approach , If the above both the approaches are for the same reason do we have any advantage one on another ?

Thanks for your help and checking this

Regards

Yii Fan

Why do you think the first approach should use session? It shouldn’t, and it doesn’t.

All it does is store the data in that object’s variable so that subsequent calls on the same page/request won’t query the database.

It has nothing to do with session, while setState() specifically handles session data.

Every time - what does that mean?

A PHP application exists only for one request.

A new request will rebuild everything. Remember that.

So _db_user will always be false.

Thank You jacome and JellYsandwich for your response . Excuse me If I am wrong

Yii::app()->user->set/getState() is basically deals with CWebUser . This is one of the ways to handle sessions . Is this not same as extending CWebUser and adding a attributes .

Regards

Yii Fan