Model scopes get processed by Yii even when you don't use the scope?!

So I ran into an interesting problem. I’m using scopes to return data that only belongs to a user, base on his account ID.




	public function scopes() {

		return array(

			'user'=>array(

				'condition'=>"accountId=".Yii::app()->user->accountId,

			),

			'active'=>array(

				'condition'=>"active=1",

			),

		);

	}



I have 2 different apps accessing the model. One of the apps has no idea about users, logins, etc…its just pulling data. The other app is the front end, which needs to only show the user their own data.

However the problem comes in that the USER scope causes the first app to error, but it isn’t even being used. Is this the way it should work? My guess is the failure is because Yii::app()->user doesn’t exist in the first app, but always exists in the second.

Still, I’m puzzled as to why Yii would even look at the scope if it isn’t being used.

Thoughts?

Thanks,

Matt

Update:

I found this in my searching: http://www.yiiframework.com/wiki/30/how-to-add-a-named-scope-to-activerecords-with-a-behavior/

Seems to be a reasonable answer, I’m going to try to implement and see what happens!

-Matt

Hi,

probably in your case usage of behavior is unneeded overhead, you can achieve the same result just using method-based named scope inside you AR model class.