Yii 1.1.7 Bug in ActiveRecord __sleep

Hi guys,

I think this is a bug - not 100% sure but it has solved my problem.

I was having a problem when caching data, that whenever I was trying to create a new AR object, it would keep telling me that the meta data of the object is null, and give an error.

After some detective work it seems that caching the object leads to it getting serialized, which leads to the function __sleep being called on the AR object, which makes the meta data null.

This seems to be a problem in the case that the AR object being serialized is a result of AR::model() function, since the model’s meta data should never be nullified (since all instances of this AR rely on that meta data).

My solution is as follows:

public function __sleep()




	return array_keys((array)$this);


Here, we unset the self::$models object of this class when sleeping, in order for the meta data to be rebuilt when it is needed. Otherwise meta data will remain null for this http request and cause problems.

Anyone knowledgeable about Yii (quang for example), please can you check this and see if it is the right solution, or completely off? In any case it has solved my problems… (hope it didn’t create any new ones!)

Can you provide some code to reproduce it locally?

Here is the code:

In controller:

public function actionYiiBug()




	Yii::app()->cache->set('mod', $model);



When last line is run there will be a PHP error "Trying to get property of non-object".

Also, please make sure to set cache component to CMemCache or something (if not it won’t serialize object).

Now, I realize that Users::model() should probably not be cached but this happens a number of places in my code (outsourced developers), and I guess is something that can happen without people realizing. I think the code should be able to handle this case without returning an error, since it took me a long time to understand what’s going on. Maybe it will save others the hassle.


Is no-one else experiencing this issue?

Sorry for no response for a long time. I’ve evaluated this issue. The problem is that you’re caching model finder (Users::model()) instead of model instance (new Users).