Last active
August 29, 2015 14:17
-
-
Save SebLours/475b31771e468f4213f7 to your computer and use it in GitHub Desktop.
Simple adaptation of the Doctrine ORM Paginator (Doctrine\ORM\Tools\Pagination\Paginator) for Doctrine ODM MongoDB
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 AppBundle\Doctrine\ODM\Pagination; | |
use Doctrine\ODM\MongoDB\Query\Builder; | |
use Doctrine\ODM\MongoDB\Query\Query; | |
/** | |
* Simple adaptation of Doctrine\ORM\Tools\Pagination\Paginator | |
* for Doctrine ODM MongoDB | |
*/ | |
class Paginator implements \Countable, \IteratorAggregate | |
{ | |
/** | |
* @var Doctrine\ODM\MongoDB\Query\Query | |
*/ | |
protected $query; | |
/** | |
* Constructor. | |
* | |
* @param Query|Builder $query | |
*/ | |
public function __construct($query) | |
{ | |
$this->query = ($query instanceof Builder) ? $query->getQuery() : $query; | |
} | |
/** | |
* Returns the query. | |
* | |
* @return Query | |
*/ | |
public function getQuery() | |
{ | |
return $this->query; | |
} | |
/** | |
* @return integer | |
*/ | |
public function count() | |
{ | |
return $this->query->count(); | |
} | |
/** | |
* @return ArrayIterator | |
*/ | |
public function getIterator() | |
{ | |
return new \ArrayIterator($this->query->execute()->toArray(false)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment