Scope persistance?

I recently discovered that my app was retaining a scope definition from a previous query. For example:


$model = Model::model()->filter()->findByAttributes(array(

  'id' => $id

));

and later in the script, called from another method.


$model = Model::model()->findByAttributes(array(

  'id' => $id

));

From the debug I noticed that the second query also includes the filter scope definition. This is not desired.

I have changed the second call to create a new model object and then use that for geenrating the query.


$model = new Model();

$model = $model()->findByAttributes(array(

  'id' => $id

));

The thinking was that the ‘class’ method is retaining state, so I need to create a fresh model/method. This feels dirty and wrong, imo.

So my question is, is this intended behaviour? Am I doing something wrong? What is best practise to resolve this problem?

Code shouldn’t behave like that, check if your queries are correct(enable logs).

You can use resetScope() method from AR to reset all scopes, which mean in next call of same model, filter() scope will not be available, but it can help you not to create model again.