0

How to bind Yii:app()->user to the model User. That there was a connection type: Yii::app()->user->getUser()

For example, I want to get the currently logged in user's email:

Yii::app()->user->getUser()->email;

I readed wiki post:http://www.yiiframework.com/wiki/80/add-information-to-yii-app-user-by-extending-cwebuser-better-version/

tereško
  • 58,060
  • 25
  • 98
  • 150
frops
  • 2,196
  • 4
  • 29
  • 42

2 Answers2

2

MyWbuser.php in components:

class MyWebUser extends CWebUser{

    private $_profile = null;
    public $loginUrl='/';

    public function init(){
       parent::init();
       if(!$this->getIsGuest()){
            $this->_profile = User::model()->findByPk($this->getId());
       }
    }
    public function getProfile(){
       return $this->_profile;
   }
}

In config.php:

    'user' => array(
        'class' => 'MyWebUser',
    )

Then you can use:

Yii::app()->user->profile->name
HarryFink
  • 1,010
  • 1
  • 6
  • 6
1

Try this add this code to your useridentity authenticate function

$this->setState('email',$this->email);

And use to access that email anywhere as shown

Yii::app()->user->getState('email');
Ninad
  • 1,871
  • 3
  • 15
  • 23