The CArrayDataProvider problem of Yii

Recently I started digging in the YiiFramework. The first things I want to know about the framework were the basics – how the MVC is accomplished, database connections, ready to use component and so on. As the development team is actually the team that was engaged with the Prado framework before, I assumed that Yii will be very similar which actually is not very true. Well I won’t talk now about all the stuff I noticed and I will go straight to one of the first things I got a little irritating.

The thing is that all core components are bind or connected in some way to Active Records which is something I don’t use. That is a choice I have made and you can not sue me for that. I don’t like the concept and prefer to make my own queries – perhaps this is some for another post.

This is the case with the CArrayDataProvider. Actually they have CActiveDataProvider which is bind to AR and working like a charm. It is making two queries one with the data and one with the count of rows so you get a nice pagination component when using with a gridview or listview. Well this is not the case with CArrayDataProvider. They didn’t assume that someone like me would want to make himself the two queries and set the current page, total count of raws and pass the excerpt of them. What they actually do is using the array_slice method to get the needed portion of the page when using with a CPager. Well I can see that this has been done in the old ages. The component needs all the records from the database and will cut off the needed portion. This is insane. That is why I modified it a little bit … yeah it is almost nothing so it suits my needs. Just extend the CArrayDataProvider and include it.

class ArrayDataProvider extends CArrayDataProvider
	 * Fetches the data from the persistent data storage.
	 * @return array list of data items
	protected function fetchData()
		if(($sort=$this->getSort())!==false && ($order=$sort->getOrderBy())!='')

			return $this->rawData;
			//return array_slice($this->rawData, $pagination->getOffset(), $pagination->getLimit());
			return $this->rawData;


$viewData['dataProvider'] = new ArrayDataProvider(
				'totalItemCount' => $array['count'],
					'pageSize' => 10,

This post is also available in: Bulgarian

    • Muhammad Shoaib
    • March 9th, 2012 7:46pm

    You, sir, are awesome. I was facing exact same issue was almost about to kill myself as the number of records were 1000+ and i had to make an array as there was some custom processing of attributes needed to be done that couldn’t fit under afterFind().

    • James
    • June 5th, 2013 12:27pm

    It doesnt work with yii last version. Cgridview show all records.

      • igi
      • November 14th, 2013 5:46am

      This is working with me like CHARM. I just did a little bit modification before I return the $this->rawData, I reset the indexs.

      $this->rawData = array_values($this->rawData);

      Hope, it helps

    • igi
    • November 14th, 2013 5:09am

    WOW…. This is great sir. You saved my day.

  1. on the other hand, you could find inexpensive features allow me to explain allow a topline model and you desire to create quilts just for your special Hermes Evelyne travelling bag include. cbcegecdbecdaecb

  2. Very nice site!

  3. I thanked this advice . Today , esoteric something , I like it! Thank you for the article of your management . I enjoyed it absolutely .

  4. I am animated to bolt abstraction from your article . I have advice accept an analytic I have become because of the time continued it .

  5. Hello!

  6. Hello!

  7. Hello!

  8. Hello!

  9. Hello!

  10. Hello!

  11. Hello!

  12. Hello!

    • fast
    • April 29th, 2015 2:53pm


  13. Right now it looks like WordPress is the top blogging platform available right now. from what I’ve read Is that what you’re using on your blog? abcededcfcfbgdbg

  14. Hello!

  15. Hello!

  16. led street lighting OAK LED Co ,Limited is one of the top level China LED street lighting manufacturers and suppliers with one of the famous LED street lighting brands, as a professional company, we have our own factory, which is able to produce high quality products with reliable quality led street lighting

  17. Hello!!!

  18. I am so grateful for your blog article.Really thank you! Fantastic. dfbkfkdddfcdkcag

  1. No trackbacks yet.