ProxyQuery.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\AdminBundle\Datagrid\ORM;
  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. public function __construct(QueryBuilder $queryBuilder)
  23. {
  24. $this->queryBuilder = $queryBuilder;
  25. }
  26. public function execute(array $params = array(), $hydrationMode = null)
  27. {
  28. // always clone the original queryBuilder
  29. $queryBuilder = clone $this->queryBuilder;
  30. // todo : check how doctrine behave, potential SQL injection here ...
  31. if ($this->getSortBy()) {
  32. $sortBy = $this->getSortBy();
  33. if (strpos($sortBy, '.') === false) { // add the current alias
  34. $sortBy = $queryBuilder->getRootAlias().'.'.$sortBy;
  35. }
  36. $queryBuilder->orderBy($sortBy, $this->getSortOrder());
  37. }
  38. return $this->getFixedQueryBuilder($queryBuilder)->getQuery()->execute($params, $hydrationMode);
  39. }
  40. /**
  41. * This method alters the query to return a clean set of object with a working
  42. * set of Object
  43. *
  44. * @param \Doctrine\ORM\QueryBuilder $queryBuilder
  45. * @return void
  46. */
  47. private function getFixedQueryBuilder(QueryBuilder $queryBuilder)
  48. {
  49. $queryBuilderId = clone $queryBuilder;
  50. // step 1 : retrieve the targeted class
  51. $from = $queryBuilderId->getDQLPart('from');
  52. $class = $from[0]->getFrom();
  53. // step 2 : retrieve the column id
  54. $idName = current($queryBuilderId->getEntityManager()->getMetadataFactory()->getMetadataFor($class)->getIdentifierFieldNames());
  55. // step 3 : retrieve the different subjects id
  56. $select = sprintf('%s.%s', $queryBuilderId->getRootAlias(), $idName);
  57. $queryBuilderId->resetDQLPart('select');
  58. $queryBuilderId->add('select', 'DISTINCT '.$select);
  59. $results = $queryBuilderId->getQuery()->execute(array(), Query::HYDRATE_ARRAY);
  60. $idx = array();
  61. $connection = $queryBuilder->getEntityManager()->getConnection();
  62. foreach($results as $id) {
  63. $idx[] = $connection->quote($id[$idName]);
  64. }
  65. // step 4 : alter the query to match the targeted ids
  66. if (count($idx) > 0) {
  67. $queryBuilder->andWhere(sprintf('%s IN (%s)', $select, implode(',', $idx)));
  68. $queryBuilder->setMaxResults(null);
  69. $queryBuilder->setFirstResult(null);
  70. }
  71. return $queryBuilder;
  72. }
  73. public function __call($name, $args)
  74. {
  75. return call_user_func_array(array($this->queryBuilder, $name), $args);
  76. }
  77. public function setSortBy($sortBy)
  78. {
  79. $this->sortBy = $sortBy;
  80. }
  81. public function getSortBy()
  82. {
  83. return $this->sortBy;
  84. }
  85. public function setSortOrder($sortOrder)
  86. {
  87. $this->sortOrder = $sortOrder;
  88. }
  89. public function getSortOrder()
  90. {
  91. return $this->sortOrder;
  92. }
  93. public function getSingleScalarResult()
  94. {
  95. $query = $this->queryBuilder->getQuery();
  96. return $query->getSingleScalarResult();
  97. }
  98. public function __clone()
  99. {
  100. $this->queryBuilder = clone $this->queryBuilder;
  101. }
  102. public function getQueryBuilder()
  103. {
  104. return $this->queryBuilder;
  105. }
  106. function setFirstResult($firstResult)
  107. {
  108. $this->queryBuilder->setFirstResult($firstResult);
  109. }
  110. function getFirstResult()
  111. {
  112. $this->queryBuilder->getFirstResult();
  113. }
  114. function setMaxResults($maxResults)
  115. {
  116. $this->queryBuilder->setMaxResults($maxResults);
  117. }
  118. function getMaxResults()
  119. {
  120. $this->queryBuilder->getMaxResults();
  121. }
  122. }