vendor/symfony/doctrine-bridge/Form/ChoiceList/ORMQueryBuilderLoader.php line 45

Open in your IDE?
  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\ORM\QueryBuilder;
  13. /**
  14.  * Loads entities using a {@link QueryBuilder} instance.
  15.  *
  16.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  17.  * @author Bernhard Schussek <bschussek@gmail.com>
  18.  */
  19. class ORMQueryBuilderLoader implements EntityLoaderInterface
  20. {
  21.     /**
  22.      * Contains the query builder that builds the query for fetching the
  23.      * entities.
  24.      *
  25.      * This property should only be accessed through queryBuilder.
  26.      *
  27.      * @var QueryBuilder
  28.      */
  29.     private $queryBuilder;
  30.     public function __construct(QueryBuilder $queryBuilder)
  31.     {
  32.         $this->queryBuilder $queryBuilder;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function getEntities()
  38.     {
  39.         return $this->queryBuilder->getQuery()->execute();
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function getEntitiesByIds(string $identifier, array $values)
  45.     {
  46.         if (null !== $this->queryBuilder->getMaxResults() || null !== $this->queryBuilder->getFirstResult()) {
  47.             // an offset or a limit would apply on results including the where clause with submitted id values
  48.             // that could make invalid choices valid
  49.             $choices = [];
  50.             $metadata $this->queryBuilder->getEntityManager()->getClassMetadata(current($this->queryBuilder->getRootEntities()));
  51.             foreach ($this->getEntities() as $entity) {
  52.                 if (\in_array((string) current($metadata->getIdentifierValues($entity)), $valuestrue)) {
  53.                     $choices[] = $entity;
  54.                 }
  55.             }
  56.             return $choices;
  57.         }
  58.         $qb = clone $this->queryBuilder;
  59.         $alias current($qb->getRootAliases());
  60.         $parameter 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;
  61.         $parameter str_replace('.''_'$parameter);
  62.         $where $qb->expr()->in($alias.'.'.$identifier':'.$parameter);
  63.         // Guess type
  64.         $entity current($qb->getRootEntities());
  65.         $metadata $qb->getEntityManager()->getClassMetadata($entity);
  66.         if (\in_array($metadata->getTypeOfField($identifier), ['integer''bigint''smallint'])) {
  67.             $parameterType Connection::PARAM_INT_ARRAY;
  68.             // Filter out non-integer values (e.g. ""). If we don't, some
  69.             // databases such as PostgreSQL fail.
  70.             $values array_values(array_filter($values, function ($v) {
  71.                 return (string) $v === (string) (int) $v || ctype_digit($v);
  72.             }));
  73.         } elseif (\in_array($metadata->getTypeOfField($identifier), ['uuid''guid'])) {
  74.             $parameterType Connection::PARAM_STR_ARRAY;
  75.             // Like above, but we just filter out empty strings.
  76.             $values array_values(array_filter($values, function ($v) {
  77.                 return '' !== (string) $v;
  78.             }));
  79.         } else {
  80.             $parameterType Connection::PARAM_STR_ARRAY;
  81.         }
  82.         if (!$values) {
  83.             return [];
  84.         }
  85.         return $qb->andWhere($where)
  86.                   ->getQuery()
  87.                   ->setParameter($parameter$values$parameterType)
  88.                   ->getResult();
  89.     }
  90. }