Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display attributes of logged in user in yii

I want to check if what I was doing was right so I have this code...

$user= Yii::app()->user->id;
$account= $user->account_type;
print_r($account);

My table has the following attributes: account_id, username, password and account_type

but when I do the code... I get the following error:

"Trying to get property of non object"

please help, I am a beginner so I'm sorry for a basic problem...

like image 961
anyabythestars Avatar asked Nov 20 '25 09:11

anyabythestars


1 Answers

You can create a model for your table using gii, and then query the model using the user id, something like:

$user= Yii::app()->user->id;
$userModel = User::model()->findByPK($user);
$account= $userModel->account_type;

or, you can setState in your UserIdentity.php after validating username and password, like:

$this->setState('accounttype', $userModel->account_type);

and use this wherever you want just like:

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

You can check this yii wiki

Hope that helps :)

like image 142
Criesto Avatar answered Nov 22 '25 03:11

Criesto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!