Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Created February 1, 2012 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhartikainen/1715962 to your computer and use it in GitHub Desktop.
Save jhartikainen/1715962 to your computer and use it in GitHub Desktop.
Doctrine 2 pagination adapter for Zend_Paginator
<?php
namespace Wantlet\ORM;
use Zend_Paginator_Adapter_Interface;
use Doctrine\ORM\Query;
/**
* Zend_Paginator adapter for Doctrine 2 queries
*/
class QueryPaginator implements Zend_Paginator_Adapter_Interface {
private $count;
private $select;
/**
* @param \Doctrine\ORM\Query $select The query to be used for selecting the results
* @param \Doctrine\ORM\Query $count The query to be used for getting a total count of available items
*/
public function __construct(Query $select, Query $count) {
$this->select = $select;
$this->count = $count;
}
/**
* Returns an collection of items for a page.
*
* @param integer $offset Page offset
* @param integer $itemCountPerPage Number of items per page
* @return array
*/
public function getItems($offset, $itemCountPerPage) {
$this->select->setFirstResult($offset);
$this->select->setMaxResults($itemCountPerPage);
return $this->select->execute();
}
/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
*/
public function count() {
return (int)$this->count->getSingleScalarResult();
}
}
@fuhrysteve
Copy link

I don't think this is going to work when you start to do things such as grouping.

Check out https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Paginate/PaginationAdapter.php for a more complete approach.

@jhartikainen
Copy link
Author

I was about to mention that Benjamin's implementation is probably more complete but you appear to have already done that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment