Filtering Data From Cactivedataprovider

Hello all,

i have been developing a couple of projects using Yii Framework and now i have some problems on the CActiveDataProvider object.

i am talking about the search() function on the model file (this function is being used from the admin view).

Here is what i do:


$data = new CActiveDataProvider($this, array(

	'criteria'=>$criteria,

));

Instead of the usual


return new CActiveDataProvider($this, array(

	'criteria'=>$criteria,

));

  1. Getting data from CBbCriteria ($data->getData()).

  2. Filter each record ( accordingly to the permission a user has on the record, show it or don’t show it ).




$output=array();

foreach($data->getData() as $record)

{

if( My system checks if current user logged has access to view this $record->id)

    $output[]=$record;

}

  1. Once i have the $output data i want, i do a $data->setData($output); and $data->setTotalItemCount(count($output)); so basically the top-gridview shows the number of results filtered (not the total number of results).

The problem comes with pagination.

  • Initial $data from CActiveDataProvider object, returns the only the first 10 results (default number of results per page).

  • What happens if on the first 10 results, there are no $records to show but it has $records on the second page?

Basically it shows no results on the gridView first page, but the paginator shows page 1 and 2. (the first page shows no results, and the second shows records).

How could fix this?

i mean, i would like the paginator to work accordingly the $output array, instead of $data initial array, so if there are no results on the first page just show a first page with the records from the second.

Hope to get it explained correctly!

Thanks.

Isn’t there a way to make a SQL query that would return the filtered data for you ?

If not then one solution would be to create a new dataprovider for the grid instead of using setData()

for example you can create an CArrayDataProvider - http://www.yiiframew…rayDataProvider

Thanks Maurizio,

finally figured out how to do it using criteria addCondition and addInCondition methods instead of filtering $data.