Created
February 1, 2012 08:35
-
-
Save jhartikainen/1715962 to your computer and use it in GitHub Desktop.
Doctrine 2 pagination adapter for Zend_Paginator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 >= 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(); | |
} | |
} |
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
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.