dongre6270 2016-07-14 10:06
浏览 57
已采纳

未确定的属性“WebUser.group_id”

I need to get the [group_id] => 1 of current user:

echo Yii::app()->user->group_id

when I write:

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

I get:

WebUser Object ( 
    [_model:WebUser:private] => User Object ( 
        [_profile:protected] => Profile Object ( 
            [created_at] => 
            [created_by] => 
            [updated_at] => 
            [updated_by] => 
            [_new:CActiveRecord:private] => 
            [_attributes:CActiveRecord:private] => Array ( 
                [user_id] => 53 
                [firstname] => Garik 
                [lastname] => test_Lastname 
                [title] => test_title 
                [gender] => male 
                [street] => 
                [zip] => 
                [city] => 
                [country] => 
                [state] => 
                [birthday_hide_year] => 0 
                [birthday] => 2016-07-01 00:00:00 
                [about] => 
                [phone_private] => 
                [phone_work] => 
                [mobile] => 
                [fax] => 
                [im_skype] => 
                [im_msn] => 
                [im_icq] => 
                [im_xmpp] => 
                [url] => 
                [url_facebook] => 
                [url_linkedin] => 
                [url_xing] => 
                [url_youtube] => 
                [url_vimeo] => 
                [url_flickr] => 
                [url_myspace] => 
                [url_googleplus] => 
                [url_twitter] => 
            ) 
            [_related:CActiveRecord:private] => Array ( ) 
            [_c:CActiveRecord:private] => 
            [_pk:CActiveRecord:private] => 53 
            [_alias:CActiveRecord:private] => t 
            [_errors:CModel:private] => Array ( ) 
            [_validators:CModel:private] => 
            [_scenario:CModel:private] => update 
            [_e:CComponent:private] => Array ( 
                [onbeforedelete] => CList Object ( 
                    [_d:CList:private] => Array ( 
                        [0] => Array ( 
                            [0] => ActivityModule 
                            [1] => onActiveRecordDelete 
                        ) 
                        [1] => Array ( 
                            [0] => FileModuleEvents 
                            [1] => onBeforeHActiveRecordDelete 
                        ) 
                    ) 
                    [_c:CList:private] => 2 
                    [_r:CList:private] => 
                    [_e:CComponent:private] => 
                    [_m:CComponent:private] => 
                ) 
            ) 
            [_m:CComponent:private] => 
        ) 
        [created_at] => 2016-06-17 10:50:57 
        [created_by] => 27 
        [updated_at] => 2016-06-22 09:15:25 
        [updated_by] => 27 
        [_new:CActiveRecord:private] => 
        [_attributes:CActiveRecord:private] => Array ( 
            [id] => 53 
            [guid] => 397272fb-ac4f-4df4-95e7-171b34e85e04 
            [wall_id] =>
            **[group_id] => 1** 
            [status] => 1 
            [super_admin] => 1 
            [username] => test2 
            [email] => rush@................

and when I write:

echo Yii::app()->user->group_id

I get an error:

CException: 
Undetermined property “WebUser.group_id”

class WebUser

<?php

class WebUser extends CWebUser
{

    /**
     * Stores user model to not repeat the database query
     *
     * @var User
     */
    private $_model;

    /**
     * Returns the users displayname
     *
     * Access it by Yii::app()->user->displayname
     *
     * @return String
     */
    public function getDisplayName()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user->displayName;
    }

    /**
     * Returns the users e-mail address
     *
     * @return String
     */
    public function getEmail()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user->email;
    }

    /**
     * Returns the language code of the user model
     *
     * @return String
     */
    public function getLanguage()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        if ($user != null)
            return $user->language;
    }

    /**
     * Returns users guid
     *
     * @return String
     */
    public function getGuid()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user->guid;
    }

    /**
     * Returns users authentication mode
     *
     * @return String
     */
    public function getAuthMode()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user->auth_mode;
    }

    /**
     * Returns current user model
     *
     * @return type
     * @throws CHttpException
     */
    public function getModel()
    {
        $user = $this->loadUser(Yii::app()->user->id);

        if ($user == null)
            throw new CHttpException(500, 'Could not find logged in user!');

        return $user;
    }

    /**
     * Reloads the user cached model
     */
    public function reload()
    {
        $this->_model = null;
    }

    /**
     * Checks if the user is admin
     *
     * Access it by Yii::app()->user->isAdmin()
     *
     * @return int
     */
    public function isAdmin()
    {
        $user = $this->loadUser(Yii::app()->user->id);

        if ($user->super_admin == 1) {
            return true;
        }

        return false;
    }

    /**
     * Checks if the can approve new users
     * @todo Add caching support
     *
     * @return boolean
     */
    public function canApproveUsers()
    {

        if ($this->isAdmin())
            return true;

        $user = $this->loadUser(Yii::app()->user->id);

        $adminGroups = GroupAdmin::model()->countByAttributes(array('user_id' => $user->id));
        if ($adminGroups != 0) {
            return true;
        }

        return false;
    }

    /**
     * Checks if the user can create a space
     *
     * @return boolean
     */
    public function canCreateSpace()
    {
        return ($this->canCreatePrivateSpace() || $this->canCreatePublicSpace());
    }

    /**
     * Checks if the user can create public spaces
     *
     * @return boolean
     */
    public function canCreatePublicSpace()
    {
        $user = $this->loadUser(Yii::app()->user->id);

        if (Yii::app()->user->isAdmin()) {
            return true;
        } elseif ($user->group !== null && $user->group->can_create_public_spaces == 1) {
            return true;
        }

        return false;
    }

    /**
     * Checks if user can create private spaces
     *
     * @return boolean
     */
    public function canCreatePrivateSpace()
    {
        $user = $this->loadUser(Yii::app()->user->id);

        if (Yii::app()->user->isAdmin()) {
            return true;
        } elseif ($user->group !== null && $user->group->can_create_private_spaces == 1) {
            return true;
        }

        return false;
    }

    /**
     * Loads user model and store/cache it as class attribute
     *
     * @param Int $id
     * @return User
     */
    protected function loadUser($id = null)
    {

        if ($this->_model === null) {
            if ($id !== null) {
                $this->_model = User::model()->findByPk($id);
            } else {
                // Create Blank user
                $this->_model = new User();
            }
        }
        return $this->_model;
    }

    /**
     * If request is ajax, do not update auth status
     * @override
     */
    protected function updateAuthStatus()
    {
        if (Yii::app()->request->isAjaxRequest)
            return;

        parent::updateAuthStatus();


    }

}
  • 写回答

1条回答 默认 最新

  • drq9991 2016-07-14 11:43
    关注

    The group_id you're trying to reach is not a property of WebUser object but rather a property of the User model that is contained inside Webuser in the private $_model .

    So the code to access it would be

    Yii::app()->user->model->group_id;
    

    or

    Yii::app()->user->getModel()->group_id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号