DataProvider strange behavior

Good day everyone,

I have following code:


$dependency=new CDbCacheDependency('SELECT MAX(user_end_date) FROM tbl_user');			

$data=User::model()->cache(1000,$dependency)->findAll($criteria);

$DP = new CActiveDataProvider('User', array('data'=>$data,'pagination'=>false));


echo $DP->totalItemCount; //gives for example 5 and this is not expected value

echo count($DP->getData());// and this one is correct and gives for example 1

		

if($DP->totalItemCount<5){

...

  this code is not executed due to wrong value...

...

}

Is this some how connected with the cache? What could be wrong?

Thank you in advance.

You should apply the ‘$criteria’ to the dataprovider too.

$DP->totalItemCount calls User->count() with no criteria, because your dataprovider has no criteria assigned.

Seems to return all users if you have not User.defaultScope.

See the code of CActiveDataProvider.calculateTotalItemCount();

So maybe this helps:




 $DP = new CActiveDataProvider('User', array('data'=>$data,'pagination'=>false,'criteria'=>$criteria));



Thanks Joblo,

But I was disappointed to see that the CActiveDataProvider makes query even when the ‘data’ is not empty…

CArrayDataProvider is what you need.