DoctrinePager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) Jonathan H. Wage <jonwage@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\BaseApplicationBundle\Tool;
  11. use Doctrine\ORM\Query;
  12. use Doctrine\ORM\QueryBuilder;
  13. /**
  14. * Doctrine pager class.
  15. *
  16. * @author Jonathan H. Wage <jonwage@gmail.com>
  17. * @version SVN: $Id: sfDoctrinePager.class.php 28897 2010-03-30 20:30:24Z Jonathan.Wage $
  18. */
  19. class DoctrinePager extends Pager implements \Serializable
  20. {
  21. protected
  22. $query = null,
  23. $query_builder = null,
  24. $count_column = 'id';
  25. /**
  26. * Serialize the pager object
  27. *
  28. * @return string $serialized
  29. */
  30. public function serialize()
  31. {
  32. $vars = get_object_vars($this);
  33. unset($vars['query']);
  34. return serialize($vars);
  35. }
  36. /**
  37. * Unserialize a pager object
  38. *
  39. * @param string $serialized
  40. */
  41. public function unserialize($serialized)
  42. {
  43. $array = unserialize($serialized);
  44. foreach ($array as $name => $values)
  45. {
  46. $this->$name = $values;
  47. }
  48. }
  49. /**
  50. * Returns a query for counting the total results.
  51. *
  52. * @return Doctrine\ORM\Query
  53. */
  54. public function getCountQuery()
  55. {
  56. $query_builder = clone $this->getQueryBuilder();
  57. $query_builder->select(sprintf('count(%s.%s) as nb', $query_builder->getRootAlias(), $this->getCountColumn()));
  58. return $query_builder->getQuery();
  59. }
  60. public function getCountColumn()
  61. {
  62. return $this->count_column;
  63. }
  64. public function setCountColumn($count_column) {
  65. return $this->count_column = $count_column;
  66. }
  67. /**
  68. * @see Pager
  69. */
  70. public function init()
  71. {
  72. $this->resetIterator();
  73. $countQuery = $this->getCountQuery();
  74. $countQuery->setParameters($this->getParameters());
  75. $count = $countQuery->getSingleScalarResult();
  76. $this->setNbResults($count);
  77. $query = $this->getQuery();
  78. $query
  79. ->setParameters($this->getParameters())
  80. ->setFirstResult(0)
  81. ->setMaxResults(0);
  82. if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
  83. $this->setLastPage(0);
  84. }
  85. else
  86. {
  87. $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
  88. $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
  89. $query
  90. ->setFirstResult($offset)
  91. ->setMaxResults($this->getMaxPerPage());
  92. }
  93. }
  94. /**
  95. * Get the query builder for the pager.
  96. *
  97. * @return Doctrine\ORM\QueryBuilder
  98. */
  99. public function getQueryBuilder()
  100. {
  101. return $this->query_builder;
  102. }
  103. /**
  104. * Set query object for the pager
  105. *
  106. * @param Doctrine\ORM\QueryBuilder $query
  107. */
  108. public function setQueryBuilder(\Doctrine\ORM\QueryBuilder $query_builder)
  109. {
  110. $this->query_builder = $query_builder;
  111. }
  112. /**
  113. * Get the query for the pager.
  114. *
  115. * @return Doctrine\ORM\Query
  116. */
  117. public function getQuery()
  118. {
  119. if (!$this->query) {
  120. $this->query = $this->getQueryBuilder()->getQuery();
  121. }
  122. return $this->query;
  123. }
  124. /**
  125. * Retrieve the object for a certain offset
  126. *
  127. * @param integer $offset
  128. *
  129. * @return object
  130. */
  131. protected function retrieveObject($offset)
  132. {
  133. $queryForRetrieve = clone $this->getQuery();
  134. $queryForRetrieve
  135. ->setFirstResult($offset - 1)
  136. ->setMaxResults(1);
  137. $results = $queryForRetrieve->execute();
  138. return $results[0];
  139. }
  140. /**
  141. * Get all the results for the pager instance
  142. *
  143. * @param mixed $hydrationMode A hydration mode identifier
  144. *
  145. * @return array
  146. */
  147. public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
  148. {
  149. return $this->getQuery()->execute(array(), $hydrationMode);
  150. }
  151. }