Yii Framework Forum: Filtering Data From Cactivedataprovider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Filtering Data From Cactivedataprovider How to filter data from CActiveDataProvider and paginate it Rate Topic: -----

#1 User is offline   sergifm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 13-December 12

Posted 13 December 2012 - 04:30 AM

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;
}

3. 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.
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 13 December 2012 - 04:35 AM

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
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   sergifm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 13-December 12

Posted 13 December 2012 - 08:26 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users