I modified UserIdentity() class to authenticate the user against a MySQL database. I am able to verify the entered username and password.
However, I also get two more values back from the database which I’d like to store with the user.
Accessing the username with Yii::app()->user->name works as expected. I’d like to add the other two parameters to Yii::app()->user so I can access the values the same way I access the username: Yii::app()->user->parameterOne, Yii::app()->user->parameterTwo.
What is the best way to accomplish this? Do I have to modify or extend CWebUser for this?
This username behavior is the standard one within yii. I didn’t modify this. Or are you saying that it is bad to store two parameters as rookie84 suggested?
It doesn’t matter if it is standard Yii or not, you will get to the same problem as i described, so just DO NOT use it, instead do as i advised you, let Yii store only the user_id(because this will not change) in the user state and based on that user id, write methods that will bring you the extra data you need from the user table.
When you do something like you proposed you also have to remember to clear cached user model. This is the same as changing username and assigning its new value, unless you don’t have setter implemented.
Using CWebUser::setState is a good idea. With CWebUser::__get you can use it like this: