Paginator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools\Pagination;
  20. use Doctrine\ORM\QueryBuilder;
  21. use Doctrine\ORM\Query;
  22. use Doctrine\ORM\NoResultException;
  23. use Doctrine\ORM\Tools\Pagination\WhereInWalker;
  24. use Doctrine\ORM\Tools\Pagination\CountWalker;
  25. use Countable;
  26. use IteratorAggregate;
  27. use ArrayIterator;
  28. /**
  29. * Paginator
  30. *
  31. * The paginator can handle various complex scenarios with DQL.
  32. *
  33. * @author Pablo Díez <pablodip@gmail.com>
  34. * @author Benjamin Eberlei <kontakt@beberlei.de>
  35. * @license New BSD
  36. */
  37. class Paginator implements \Countable, \IteratorAggregate
  38. {
  39. /**
  40. * @var Query
  41. */
  42. private $query;
  43. /**
  44. * @var bool
  45. */
  46. private $fetchJoinCollection;
  47. /**
  48. * @var int
  49. */
  50. private $count;
  51. /**
  52. * Constructor.
  53. *
  54. * @param Query|QueryBuilder $query A Doctrine ORM query or query builder.
  55. * @param Boolean $fetchJoinCollection Whether the query joins a collection (true by default).
  56. */
  57. public function __construct($query, $fetchJoinCollection = true)
  58. {
  59. if ($query instanceof QueryBuilder) {
  60. $query = $query->getQuery();
  61. }
  62. $this->query = $query;
  63. $this->fetchJoinCollection = (Boolean) $fetchJoinCollection;
  64. }
  65. /**
  66. * Returns the query
  67. *
  68. * @return Query
  69. */
  70. public function getQuery()
  71. {
  72. return $this->query;
  73. }
  74. /**
  75. * Returns whether the query joins a collection.
  76. *
  77. * @return Boolean Whether the query joins a collection.
  78. */
  79. public function getFetchJoinCollection()
  80. {
  81. return $this->fetchJoinCollection;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function count()
  87. {
  88. if ($this->count === null) {
  89. /* @var $countQuery Query */
  90. $countQuery = $this->cloneQuery($this->query);
  91. if ( ! $countQuery->getHint(CountWalker::HINT_DISTINCT)) {
  92. $countQuery->setHint(CountWalker::HINT_DISTINCT, true);
  93. }
  94. $countQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
  95. $countQuery->setFirstResult(null)->setMaxResults(null);
  96. try {
  97. $data = $countQuery->getScalarResult();
  98. $data = array_map('current', $data);
  99. $this->count = array_sum($data);
  100. } catch(NoResultException $e) {
  101. $this->count = 0;
  102. }
  103. }
  104. return $this->count;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getIterator()
  110. {
  111. $offset = $this->query->getFirstResult();
  112. $length = $this->query->getMaxResults();
  113. if ($this->fetchJoinCollection) {
  114. $subQuery = $this->cloneQuery($this->query);
  115. $subQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker'))
  116. ->setFirstResult($offset)
  117. ->setMaxResults($length);
  118. $ids = array_map('current', $subQuery->getScalarResult());
  119. $whereInQuery = $this->cloneQuery($this->query);
  120. // don't do this for an empty id array
  121. if (count($ids) > 0) {
  122. $namespace = WhereInWalker::PAGINATOR_ID_ALIAS;
  123. $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker'));
  124. $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids));
  125. $whereInQuery->setFirstResult(null)->setMaxResults(null);
  126. foreach ($ids as $i => $id) {
  127. $i++;
  128. $whereInQuery->setParameter("{$namespace}_{$i}", $id);
  129. }
  130. }
  131. $result = $whereInQuery->getResult($this->query->getHydrationMode());
  132. } else {
  133. $result = $this->cloneQuery($this->query)
  134. ->setMaxResults($length)
  135. ->setFirstResult($offset)
  136. ->getResult($this->query->getHydrationMode())
  137. ;
  138. }
  139. return new \ArrayIterator($result);
  140. }
  141. /**
  142. * Clones a query.
  143. *
  144. * @param Query $query The query.
  145. *
  146. * @return Query The cloned query.
  147. */
  148. private function cloneQuery(Query $query)
  149. {
  150. /* @var $cloneQuery Query */
  151. $cloneQuery = clone $query;
  152. $cloneQuery->setParameters($query->getParameters());
  153. foreach ($query->getHints() as $name => $value) {
  154. $cloneQuery->setHint($name, $value);
  155. }
  156. return $cloneQuery;
  157. }
  158. }