Carraydataprovider And Pagination

So I have tried to find an answer to this but just seem to find it. I have the need to pass data to the CGridview but CActiveDataProvider is not a viable option. As such I’ve been using the CArrayDataProvider. This works just fine except that it seems to require all the data to be present in the array. While this is fine for small datasets this particular dataset can have as much as 30k records in it and storing that much data is expensive when all I want to do is show the user 10 items at time.

Here is a simplified example of how I usually build the data (in case I’ve been doing it wrong):

$query = 'SELECT pid, last_name, first_name, address FROM people ORDER BY last_name, first_name;';	

$cmd = Yii::app()->createCommand($query);

$result = $cmd->query();

$data = array();

foreach($result as $row)

	$data[] = array('pid'=>$row['pid'], 'last_name'=>$row['last_name'], 'first_name'=>$row['first_name'], 'address'=>$row['address']);

$dp =  return new CArrayDataProvider($data, 






Outside the world of Yii I would generally just use limits + offsets but I don’t see how I can do that with CArrayDataProvider. Is there a way to do this? If not is there another dataprovider class that I could use? I’m willing to extend my own dataprovider if need be but I obviously if there is a standard way of doing this that would be ideal.

Hi enotirab,


I see nothing special here that should prevent you from using CActiveDataProvider.

We have a few tables in our database that are extremely large (300+ columns). I’m not able to redesign that database at this time but it can make the CActiveRecord class to large. Also, sometimes the data in these arrays is computed on the fly and is a mix of sql query plus calculations.

That being said I was not aware of the CSqlDataProvider which provides me with the functionality I happened to be looking for right now.

I see. It’s a pity that you have to deal with that kind of database table …

I agree with you that CSqlDataProvider may be a help to your circumstance.