Where We Use Setstate

where we use


Yii::app()->user->setState($attribute,$value)

where is the best position ?

In UserIdentity or in Webuser afterlogin method?

which is the best use?

Identity seems more appropriate

Yes I am using it in identity , but sometimes it gives me an error webuser.$attribute not defined . i cant figure it out when the error occurs !!

it depends what you put into session state if its user data likely some of your users don’t have all the fields

Thank You. :)

I want to access other attributes of user table like


Yii::app()->user->id

For that i have modified UserIdentity.php with following code


$this->setState('CompanyId', $user->company_id);

when i use


Yii::app()->user->CompanyId;

,it gives me error. Property "RWebUser.CompanyId" is not defined.

What is wrong i have done?

if you have


$this->setState('CompanyId', $user->company_id);

access to it by


Yii::app()->user->getState('CompanyId');

else

if you want use Yii::app()->user->CompanyId , most rewrite webUser.php and define function CompanyId().

thanx @n-r. it helps me. :rolleyes:

This is something i often use in WebUser, it gives you access to all params by name…




public function __get($name)

    {

        try {

            return parent::__get($name);

        } catch (CException $e) {

            $m = $this->user;

            if ($m->__isset($name))

                return $m->{$name};

            else throw $e;

        }

    }


    public function __set($name, $value)

    {

        try {

            return parent::__set($name, $value);

        } catch (CException $e) {

            $m = $this->user;

            $m->{$name} = $value;

        }

    }


    public function __call($name, $parameters)

    {

        try {

            return parent::__call($name, $parameters);

        } catch (CException $e) {

            $m = $this->user;

            return call_user_func_array(array($m, $name), $parameters);

        }

    }