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?