0

I found that yii can get and echo the user name by this :

    echo Yii::app()->user->name;

my question is how can I specify the field in the table user to be bind with the CWebUser::name

mehsen
  • 588
  • 2
  • 7
  • 18

2 Answers2

2

In your config

// application components
    'components'=>array(
        'user'=>array(
            // enable cookie-based authentication 
            'allowAutoLogin'=>true,
            'class'=>'WebUser',
        ),

WebUser class (example for "email" field from table "user"):

  class WebUser extends CWebUser
    {
        public function getEmail() 
        {    
            if(!$this->getState('__email')&&$this->id)
            {
                $user = User::model()->findByPk($this->id);
                $this->setState('__email', $user->email);
            }
            $state = $this->getState('__email');
            return $state;
        }

        public function login($identity, $duration=0) 
        {
            $this->allowAutoLogin = true;
            parent::login($identity, $duration);
            $this->id = $identity->id;
            if (!$this->isGuest)
            {
                if($user = User::model()->findByPk($this->id))
                {
                    $this->setState('__email', $user->email);
                }
            }
        }
    }
Pave
  • 2,347
  • 4
  • 21
  • 23
0

You should write your own UserIdentity component by extending CUserIdentity, and in authenticate() method after user identification declare $this->username = $user->variable;

timkame
  • 26
  • 2