I have 3 tables
users( id, default_profile_id, username, password)
profiles( id, name_first, name_last, email)
staff( id, profile_id, organisation_id,is_admin)
so to get information like profile_id, organisation_id, username, user_id i am using UserIdentity class and WebUser class. Here is the WebUser class:
Code:
class WebUser extends CWebUser
{
private $_model;
public function __get($name)
{
if ($this->hasState('__userInfo')){
$user=$this->getState('__userInfo',array());
if (isset($user[$name])) {
return $user[$name];
}
}
return parent::__get($name);
}
public function login($identity, $duration){
$this->setState('__userInfo', $identity->getUser());
parent::login($identity, $duration);
}
// Return user's name.
// access it by Yii::app()->user->getName()
public function getName(){
if(Yii::app()->user->isGuest)
{
return "";
}else{
$user=$this->loadUser(Yii::app()->user->id);
if($user===null)
return 0;
else{
return $user['name_first'].' '.$user['name_last'];
}
}
}
// This is a function that checks the field 'is_admin'
// in the Staff model to be equal to 1, that means it's admin
// access it by Yii::app()->user->isAdmin()
public function isAdmin(){
$user = $this->loadUser(Yii::app()->user->id);
if($user===null)
return 0;
else
return $user['is_admin'];
}
// Return user's organisation id.
// from the Staff model,
// access it by Yii::app()->user->getOrganisationId()
public function getOrganisationId(){
$user = $this->loadUser(Yii::app()->user->id);
if($user===null)
return 0;
else
return $user['organisation_id'];
}
// Load user model
protected function loadUser($id=null){
if($this->_model===null)
{
if($id!==null)
{
$this->_model=Yii::app()->db->createCommand()
->select('name_first,name_last,profile_id,organisation_id,is_admin')
->from('staff hks')
->join('users hku','hku.default_profile_id=hks.profile_id')
->join('profiles hkp','hkp.id=hku.default_profile_id')
->where('hku.id=:id',array(':id'=>$id))
->queryRow();
}
return $this->_model;
}
}
Now when i am trying to access aboce mentioned information using:
Yii::app()->user->getName() for name of user,
Yii::app()->user->getOrganisationId() for organisation id and
Yii::app()->user->isAdmin() to check whether he is admin or not.
Then Only one of the above three is working and others are returning zero value depending which one i have written first in a particular page.
Output:
NAME: Chirag Chetan
Organisation ID: 0
isAdmin: 0
Please suggest a method to how to retrieve above information.