Posts Tagged ‘paginate’

Pagination in symfony without criteria

November 28, 2009 1 comment

I have created a news aggregator and needed to paginate the results. I had the ‘feed’ object having an array of ‘feeditems’ objects in my actions. So, I could not use sfPropelPager as it needs a criteria object to pull objects from the database. The solution which worked for me is presented here. Disclaimer: This is a complete rip-off from the snippets section on symfony site, modified slightly to work with the latest version of symfony and php. I will add another post to paginate in sfFeed2Plugin when I have time.

Add this class to lib/helper or anywhere in lib and include it in the file you need to use pagination.


class myArrayPager extends sfPager
protected $resultsArray = null;

public function __construct($class = null, $maxPerPage = 10)
parent::__construct($class, $maxPerPage);

public function init()

if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
} else {
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));

public function setResultArray($array)
$this->resultsArray = $array;

public function getResultArray()
return $this->resultsArray;

public function retrieveObject($offset) {
return $this->resultsArray[$offset];

public function getResults()
return array_slice($this->resultsArray, ($this->getPage() – 1) * $this->getMaxPerPage(), $this->maxPerPage);

In the action, instead of setting an object holding array of items, set the pager.

$empager = new myArrayPager(null, 10); //10 items per page
$empager->setResultArray($this->feed->getItems()); //my object is ‘feed’ which has ‘items’ array
$empager->setPage($request->getParameter(‘page’, 1));

$this->pager = $empager;

Implementation of view is exactly the same as with sfPropelPager or the equivalent doctrine pager.