Okay, I basically need to force Yii::app()->user to reload its information from the database. Specifically, the user’s name, because this is something that can be edited at will (with some limitations).
I have tried explicitly assigning the appropriate value to the name field, but this worked roughly 50% of the time on my home server, and even less on the commercial one (sometimes it even reverts the change!). So it seems that I need some more direct route of affecting the values, without needing the users to log out and in again. Is there a specific function to do this already?
Please post your code. Do you have a cache system enabled?
There is no caching on the system. The code is literally this:
Yii::app()->user->name = $name;
And sometimes, it works. Sometimes, it doesn’t. Sometimes it works and then reverts.
I believe it’s because the name is set in the session - which is set in the Authenticate() method. After you update the user model you’ll need to update the session info.
This article might help. Read the comments for alternatives.
As I see it, you’ll have to setUser($user) once your User Model is updated - maybe afterSave(). Just make sure you only set the current user. I.e. if admin saves a user record, don’t set that user to the current one! Make sense?
waterloomatt pointed you well.
Add a property to the user: model. This property will give you the model associated with the user.
You will be able to do:
The model? The user does have a method called getModel(), as recommended by another tutorial, that returns the CActiveRecord for the user based on the user’s ID. But it doesn’t impact the session at all, it’s strictly a DB lookup.
Yes, I have a UserIdentity class with an authenticate() method, and it has $_id and $_name properties that are set on login. And apparently I’ve got a getter for each. However, there’s no visible connection between those variables, the session, and the WebUser class. Do I have to actually manually set the session data? There’s no Yii::app()->user->refreshFromDb() or saveSession() methods?
Yes, you’re right. There’s no link between the CActiveRecord object and the WebUser. If you just need access to the name property, you’ll have to update your Yii::app()->user->name after you save your CActiveRecord.
If you need access to other User properties, do as zaccaria suggested and add:
public function getModel()
public function authenticate()
$record = User::model()->findByAttributes(array('username' => $this->username));
$this->model = $record;
You’ll then be able to access
and all other User properties.
Better to do like that:
public function getModel()
if ((!$this->_model)&& $this->id)
$this->model=User::model()->findByAttributes(array('username' => $this->id));
This implementation is a singleton for the user.
This. This is precisely what I want to do. Saving the model works, getting the model works. Yii::app()->user->getModel()->anything works as it should, and always has.
Yii::app()->user->name = $variable does not normally work, so I need to know the proper method of updating the user entity. Not the user model, the actual entity.
in your afterSave() method of your user model.