Search code examples
phpyiiyii-extensions

Yii User management, display user profile info


Hi I am using yii with the yii-user-management extension

I can see how it's fairly simple to get some of the current logged user info which is stored in the users table (e.g. Yii::app()->user->name)

However I was wondering how would it be possible to get related data of the current logged user (e.g. user email which is stored in the profiles tables)

in the YumUser.php model file there is a relation

$relations['profile'] = array(self::HAS_ONE, 'YumProfile', 'user_id');

However I am not sure how to use this directly in a View file


Solution

  • I believe a cleaner way to do this is suggested by the YUM docs. There is a data() method in YumWebUser that makes the user model accessible from the WebUser instance:

    // Use this function to access the AR Model of the actually
    // logged in user, for example
    public function data() {
        if($this->_data instanceof YumUser)
            return $this->_data;
        else if($this->id && $this->_data = YumUser::model()->findByPk($this->id))
            return $this->_data;
        else
            return $this->_data = new YumUser();
    }
    

    So, you should be able to simply use:

    <?php echo Yii::app()->user->data()->profile->firstname; ?>
    <?php echo Yii::app()->user->data()->profile->email; ?>