ORMQueryBuilderLoader.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Bridge\Doctrine\Form\ChoiceList;
  11. use Doctrine\DBAL\Connection;
  12. use Doctrine\DBAL\Types\ConversionException;
  13. use Doctrine\DBAL\Types\Type;
  14. use Doctrine\ORM\QueryBuilder;
  15. use Symfony\Component\Form\Exception\TransformationFailedException;
  16. /**
  17. * Loads entities using a {@link QueryBuilder} instance.
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. */
  22. class ORMQueryBuilderLoader implements EntityLoaderInterface
  23. {
  24. /**
  25. * Contains the query builder that builds the query for fetching the
  26. * entities.
  27. *
  28. * This property should only be accessed through queryBuilder.
  29. *
  30. * @var QueryBuilder
  31. */
  32. private $queryBuilder;
  33. public function __construct(QueryBuilder $queryBuilder)
  34. {
  35. $this->queryBuilder = $queryBuilder;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getEntities()
  41. {
  42. return $this->queryBuilder->getQuery()->execute();
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getEntitiesByIds(string $identifier, array $values)
  48. {
  49. if (null !== $this->queryBuilder->getMaxResults() || null !== $this->queryBuilder->getFirstResult()) {
  50. // an offset or a limit would apply on results including the where clause with submitted id values
  51. // that could make invalid choices valid
  52. $choices = [];
  53. $metadata = $this->queryBuilder->getEntityManager()->getClassMetadata(current($this->queryBuilder->getRootEntities()));
  54. foreach ($this->getEntities() as $entity) {
  55. if (\in_array((string) current($metadata->getIdentifierValues($entity)), $values, true)) {
  56. $choices[] = $entity;
  57. }
  58. }
  59. return $choices;
  60. }
  61. $qb = clone $this->queryBuilder;
  62. $alias = current($qb->getRootAliases());
  63. $parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;
  64. $parameter = str_replace('.', '_', $parameter);
  65. $where = $qb->expr()->in($alias.'.'.$identifier, ':'.$parameter);
  66. // Guess type
  67. $entity = current($qb->getRootEntities());
  68. $metadata = $qb->getEntityManager()->getClassMetadata($entity);
  69. if (\in_array($type = $metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'])) {
  70. $parameterType = Connection::PARAM_INT_ARRAY;
  71. // Filter out non-integer values (e.g. ""). If we don't, some
  72. // databases such as PostgreSQL fail.
  73. $values = array_values(array_filter($values, function ($v) {
  74. return (string) $v === (string) (int) $v || ctype_digit($v);
  75. }));
  76. } elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) {
  77. $parameterType = Connection::PARAM_STR_ARRAY;
  78. // Like above, but we just filter out empty strings.
  79. $values = array_values(array_filter($values, function ($v) {
  80. return '' !== (string) $v;
  81. }));
  82. // Convert values into right type
  83. if (Type::hasType($type)) {
  84. $doctrineType = Type::getType($type);
  85. $platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
  86. foreach ($values as &$value) {
  87. try {
  88. $value = $doctrineType->convertToDatabaseValue($value, $platform);
  89. } catch (ConversionException $e) {
  90. throw new TransformationFailedException(sprintf('Failed to transform "%s" into "%s".', $value, $type), 0, $e);
  91. }
  92. }
  93. unset($value);
  94. }
  95. } else {
  96. $parameterType = Connection::PARAM_STR_ARRAY;
  97. }
  98. if (!$values) {
  99. return [];
  100. }
  101. return $qb->andWhere($where)
  102. ->getQuery()
  103. ->setParameter($parameter, $values, $parameterType)
  104. ->getResult();
  105. }
  106. }