CActiverDataProvider forgets named scopes after call to getTotalItemCount()

Here is a modified piece of code from my current project. I may have some errors as it purely right from the brain.

In the model:




/**

 * ...

 * @property integer $hidden 

 * ...

 */

class Person {

  ...

  public function scopes() {

    return array(

      'active' => array(

        'condition' => 'hidden = 0',

      ),

    );

  }

  ...

}



In the action method:





public function actionView() {

  ...

  $staticModel = Person::model()->hidden();

  $provider = new CActiveDataProvider($staticModel);

  $this->render('view', array(

    'provider' => $provider,

  ));

}



In the view:




<? if ($provider->getTotalItemCount() == 0) { ?>

<p class="none">No active persons found</p>

<? } else { ?>

<?

$this->widget('CListView', array(

  'dataProvider' => $provider,

  'itemView' => '_personListView',

));

?>



The problem is that in the list all persons are shown, not only those whose `hidden’ attribute is set to 0.

I have dug the internals of CActiverRecord and found that CActiverDataProvider::getTotalItemCount() calls indirectly CActiverRecord::applyScopes() and applyScope() removes (sets to null) criteria applied to static model, effectively disabling named scopes on subsequent calls to CActiveDataProvider methods. In applyScopes() I have commented "$this->_c=null;" line of code to make my piece of code to work as intended, but it is not a clean fix, as it changes the documented behavior of applyScopes().

I think this is a bug, as the documentation for CActiveDataProvider::getTotalItemCount() does not state that it changes the state of the provider. Also, the indirectly called CActiveRecord::count() also does not state that it changes the state of the static model.

Anyone?

Another problem from the similar area:

$staticModel1 = Person::model()->active()

$staticModel2 = Person::model()

It happens that $staticModel1 and $staticModel2 are actualy the same PHP object. Is it documented somewhere?

Should not autogenerated scope methods and model() method cache static models with "per class and scopes" keys, not just "per class" keys?

"Global" static models "do not feel to be good" when using named scopes with static models, especially when you have several CActiveDataProvider-powered list views for the same data type on the same page…

Regarding your first question, maybe you can try to replace ‘condition = 0’ with “condition =‘0’”… I have also encountered this weird MySQL behavior (so it’s not a Yii bug) that many records, including those which are not marked as 0 are being displayed. But when I enclosed 0 inside ‘’, the results I got corrected.

Upgraded to 1.1.7.

The issue seems to be gone.