On the cookbook "How to add more information to Yii::app()->user"

I’m trying to implement a certain persistent data that belongs to a specific user according to the cookbook page of How to add more information to Yii::app()->user.

Let us assume that the Yii::app()-user has the flag authenticateMode.

It is set in the `protected/components/UserIdentity.php' as the cookbook says:



<?php


  :


  public function authenticate()


  {


  :


    if ($user===null) {


        if (isset(Yii::app()->user->authenticateMode) && Yii::app()->user->authenticateMode == self::REGISTRATION) {


  (1)


    } else {


        $this->setState('authenticateMode', self::REGISTRATION);


  (2)


    }


Obviously, since it is a persistent data, I should not initialize this flag. I wanted to do (2) first, then (1) next time according to this flag.

Then, I got a following error.

Quote

CException

Description

Property "CWebUser.authenticateMode" is not defined.

Source File

/var/www/html/yii/framework/web/auth/CWebUser.php(88)

00076:    /**

00077:      * PHP magic method.

00078:      * This method is overriden so that persistent states can be accessed like properties.

00079:      * @param string property name

00080:      * @return mixed property value

00081:      * @since 1.0.3

00082:      */

00083:    public function __get($name)

00084:    {

00085:        if($this->hasState($name))

00086:            return $this->getState($name);

00087:        else

00088: return parent::__get($name);

00089:    }

00090:

00091:    /**

00092:      * PHP magic method.

00093:      * This method is overriden so that persistent states can be set like properties.

00094:      * @param string property name

00095:      * @param mixed property value

00096:      * @since 1.0.3

00097:      */

00098:    public function __set($name,$value)

00099:    {

00100:        if($this->hasState($name))

Stack Trace

#0 /var/www/html/yii/framework/web/auth/CWebUser.php(88): CComponent->__get('authenticateMod…')

#1 /var/www/html/yii/demos/test/protected/components/UserLogin.php(19): CWebUser->__get('authenticateMod…')

The code of the portlet of 'UserLogin' is as follows.



<?php





class UserLogin extends Portlet


{


        const AUTHENTICATION=0;


        const REGISTRATION=1;





        public $title='Login';





        protected function renderContent()


        {


                $form=new LoginForm;


                if(isset($_POST['LoginForm']))


                {


                        $form->attributes=$_POST['LoginForm'];


                        if($form->validate('login'))


                                $this->controller->refresh();


                }


exception -->   if (Yii::app()->user->authenticateMode == self::AUTHENTICATION)


                        $this->render('userLogin',array('form'=>$form));


                else


                        $this->render('userLoginReg',array('form'=>$form));





        }


}


How do I use this persistent data without initializing it?

I think before you access authenticateMode, you need to check Yii::app()->user->isGuest because the state is only available for authenticated user.

Quote

I think before you access authenticateMode, you need to check Yii::app()->user->isGuest because the state is only available for authenticated user.

Thanks for your suggestion.

It means I should not use this persistent storage for such user status, right? Because this user status implies followings;

0:  Before inputting username/password (not authenticated yet).

1:  After user name error (registration mode, not authenticated yet).

(2): After authenticated (not used).

It seems to be better for me to find another storage.

You still can use Yii::app()->session[$param] = $value. See here http://www.yiiframew…pi/CHttpSession

I'm not sure if you need to use setState() for the velues you already set in session - give it a try.

You can also use CDbHttpSession to speed up your application under high load. See my blog for details.

Hi Konstantin,

Quote

You still can use Yii::app()->session[$param] = $value. See here http://www.yiiframew...pi/CHttpSession

I'm not sure if you need to use setState() for the velues you already set in session - give it a try.

You can also use CDbHttpSession to speed up your application under high load. See my blog for details.

Thanks. :)

In terms of the persistent storage, I can think of three things IMHO, a session storage that you addressed above, an ajax transmission to the remote storage and a local storage that I had used to store the widget status as shown in the forum.

Now I continue to develop a new UserLogin widget using the last one.

As for me, Yii::app()->session[$param] is much faster than Yii::app()->user->param.