Object with relations in session

Hello guys!

I have my user profile as an object and if user is logged in this object is saved in session. All seems to work not bad but except relational objects. So for example - My profile has relation ‘has one’ to country object. And when user is logging in and loads his country (relation) it is for example Country1. Then user goes to his profile page, updates his profile with new country - Country2. From now user object has country_id field value equal to Country2 id, but relational country object is still Country1

Any ideas how to solve this? Maybe I should some how to drop loaded relational objects?

This is related to the lazy loading approach and caching to save DB requests.

If you call


The magic method __get will be called and yii will store the result in the array _related for furhter needs. If you now save it to the session the array _related still has the old relation present in the _related array thus no new query will be executed.

For the object in the session you can use

  $obj->getRelated('relation', true);

Brilliant idea …

But some how this doesn't helped me. It still returning same old Country1, I've checked my log, and there is query for my related object, but it is queried using old field Id, by which this record is made related…

So I think I missed something in docs…

Will keep digging, thank you very much Dave

Finally I've solved the problem. It is Yii bug …

It doesn't mind $refresh parameter…

Strange thing, query is correct but getRelated returns old object )