我需要获得当前用户的[group_id] => 1
:
echo Yii::app()->user->group_id
我写的时候:
print_r(Yii::app()->user);
我明白了:
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@................
当我写信时:
echo Yii::app()->user->group_id
我收到错误:
CException:
Undetermined property “WebUser.group_id”
类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();
}
}
答案 0 :(得分:1)
您尝试访问的group_id
不是WebUser
对象的属性,而是User
模型的属性,该属性包含在Webuser
内{ {1}}。
所以访问它的代码是
private $_model
或
Yii::app()->user->model->group_id;