ProxyQuery.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\DoctrineORMAdminBundle\Datagrid;
  11. use Doctrine\ORM\QueryBuilder;
  12. use Doctrine\ORM\Query;
  13. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  14. /**
  15. * This class try to unify the query usage with Doctrine
  16. */
  17. class ProxyQuery implements ProxyQueryInterface
  18. {
  19. protected $queryBuilder;
  20. protected $sortBy;
  21. protected $sortOrder;
  22. protected $parameterUniqueId;
  23. public function __construct(QueryBuilder $queryBuilder)
  24. {
  25. $this->queryBuilder = $queryBuilder;
  26. $this->uniqueParameterId = 0;
  27. }
  28. public function execute(array $params = array(), $hydrationMode = null)
  29. {
  30. // always clone the original queryBuilder
  31. $queryBuilder = clone $this->queryBuilder;
  32. // todo : check how doctrine behave, potential SQL injection here ...
  33. if ($this->getSortBy()) {
  34. $sortBy = $this->getSortBy();
  35. if (strpos($sortBy, '.') === false) { // add the current alias
  36. $sortBy = $queryBuilder->getRootAlias().'.'.$sortBy;
  37. }
  38. $queryBuilder->orderBy($sortBy, $this->getSortOrder());
  39. }
  40. return $this->getFixedQueryBuilder($queryBuilder)->getQuery()->execute($params, $hydrationMode);
  41. }
  42. /**
  43. * This method alters the query to return a clean set of object with a working
  44. * set of Object
  45. *
  46. * @param \Doctrine\ORM\QueryBuilder $queryBuilder
  47. * @return void
  48. */
  49. private function getFixedQueryBuilder(QueryBuilder $queryBuilder)
  50. {
  51. $queryBuilderId = clone $queryBuilder;
  52. // step 1 : retrieve the targeted class
  53. $from = $queryBuilderId->getDQLPart('from');
  54. $class = $from[0]->getFrom();
  55. // step 2 : retrieve the column id
  56. $idName = current($queryBuilderId->getEntityManager()->getMetadataFactory()->getMetadataFor($class)->getIdentifierFieldNames());
  57. // step 3 : retrieve the different subjects id
  58. $select = sprintf('%s.%s', $queryBuilderId->getRootAlias(), $idName);
  59. $queryBuilderId->resetDQLPart('select');
  60. $queryBuilderId->add('select', 'DISTINCT '.$select);
  61. //for SELECT DISTINCT, ORDER BY expressions must appear in select list
  62. /* Consider
  63. SELECT DISTINCT x FROM tab ORDER BY y;
  64. For any particular x-value in the table there might be many different y
  65. values. Which one will you use to sort that x-value in the output?
  66. */
  67. // todo : check how doctrine behave, potential SQL injection here ...
  68. if ($this->getSortBy()) {
  69. $sortBy = $this->getSortBy();
  70. if (strpos($sortBy, '.') === false) { // add the current alias
  71. $sortBy = $queryBuilderId->getRootAlias().'.'.$sortBy;
  72. }
  73. $queryBuilderId->addSelect($sortBy);
  74. }
  75. $results = $queryBuilderId->getQuery()->execute(array(), Query::HYDRATE_ARRAY);
  76. $idx = array();
  77. $connection = $queryBuilder->getEntityManager()->getConnection();
  78. foreach($results as $id) {
  79. $idx[] = $connection->quote($id[$idName]);
  80. }
  81. // step 4 : alter the query to match the targeted ids
  82. if (count($idx) > 0) {
  83. $queryBuilder->andWhere(sprintf('%s IN (%s)', $select, implode(',', $idx)));
  84. $queryBuilder->setMaxResults(null);
  85. $queryBuilder->setFirstResult(null);
  86. }
  87. return $queryBuilder;
  88. }
  89. public function __call($name, $args)
  90. {
  91. return call_user_func_array(array($this->queryBuilder, $name), $args);
  92. }
  93. public function setSortBy($sortBy)
  94. {
  95. $this->sortBy = $sortBy;
  96. }
  97. public function getSortBy()
  98. {
  99. return $this->sortBy;
  100. }
  101. public function setSortOrder($sortOrder)
  102. {
  103. $this->sortOrder = $sortOrder;
  104. }
  105. public function getSortOrder()
  106. {
  107. return $this->sortOrder;
  108. }
  109. public function getSingleScalarResult()
  110. {
  111. $query = $this->queryBuilder->getQuery();
  112. return $query->getSingleScalarResult();
  113. }
  114. public function __clone()
  115. {
  116. $this->queryBuilder = clone $this->queryBuilder;
  117. }
  118. public function getQueryBuilder()
  119. {
  120. return $this->queryBuilder;
  121. }
  122. public function setFirstResult($firstResult)
  123. {
  124. $this->queryBuilder->setFirstResult($firstResult);
  125. }
  126. public function getFirstResult()
  127. {
  128. $this->queryBuilder->getFirstResult();
  129. }
  130. public function setMaxResults($maxResults)
  131. {
  132. $this->queryBuilder->setMaxResults($maxResults);
  133. }
  134. public function getMaxResults()
  135. {
  136. $this->queryBuilder->getMaxResults();
  137. }
  138. public function getUniqueParameterId()
  139. {
  140. return $this->uniqueParameterId++;
  141. }
  142. }