vendor/friendsofsymfony/elastica-bundle/Doctrine/ORM/Provider.php line 9

Open in your IDE?
  1. <?php
  2. namespace FOS\ElasticaBundle\Doctrine\ORM;
  3. use Doctrine\ORM\QueryBuilder;
  4. use FOS\ElasticaBundle\Doctrine\AbstractProvider;
  5. use FOS\ElasticaBundle\Exception\InvalidArgumentTypeException;
  6. @trigger_error(sprintf('The %s class is deprecated since version 4.1 and will be removed in 5.0.'Provider::class), E_USER_DEPRECATED);
  7. /**
  8.  * @deprecated since 4.1 will be removed in 5.x. Use PagerProvider instead
  9.  */
  10. class Provider extends AbstractProvider
  11. {
  12.     const ENTITY_ALIAS 'a';
  13.     /**
  14.      * Disables logging and returns the logger that was previously set.
  15.      *
  16.      * @return mixed
  17.      */
  18.     protected function disableLogging()
  19.     {
  20.         $configuration $this->managerRegistry
  21.             ->getManagerForClass($this->objectClass)
  22.             ->getConnection()
  23.             ->getConfiguration();
  24.         $logger $configuration->getSQLLogger();
  25.         $configuration->setSQLLogger(null);
  26.         return $logger;
  27.     }
  28.     /**
  29.      * Reenables the logger with the previously returned logger from disableLogging();.
  30.      *
  31.      * @param mixed $logger
  32.      *
  33.      * @return mixed
  34.      */
  35.     protected function enableLogging($logger)
  36.     {
  37.         $configuration $this->managerRegistry
  38.             ->getManagerForClass($this->objectClass)
  39.             ->getConnection()
  40.             ->getConfiguration();
  41.         $configuration->setSQLLogger($logger);
  42.     }
  43.     /**
  44.      * {@inheritDoc}
  45.      */
  46.     protected function countObjects($queryBuilder)
  47.     {
  48.         if (!$queryBuilder instanceof QueryBuilder) {
  49.             throw new InvalidArgumentTypeException($queryBuilder'Doctrine\ORM\QueryBuilder');
  50.         }
  51.         /* Clone the query builder before altering its field selection and DQL,
  52.          * lest we leave the query builder in a bad state for fetchSlice().
  53.          */
  54.         $qb = clone $queryBuilder;
  55.         $rootAliases $queryBuilder->getRootAliases();
  56.         return $qb
  57.             ->select($qb->expr()->count($rootAliases[0]))
  58.             // Remove ordering for efficiency; it doesn't affect the count
  59.             ->resetDQLPart('orderBy')
  60.             ->getQuery()
  61.             ->getSingleScalarResult();
  62.     }
  63.     /**
  64.      * This method should remain in sync with SliceFetcher::fetch until it is deprecated and removed.
  65.      *
  66.      * {@inheritDoc}
  67.      */
  68.     protected function fetchSlice($queryBuilder$limit$offset)
  69.     {
  70.         if (!$queryBuilder instanceof QueryBuilder) {
  71.             throw new InvalidArgumentTypeException($queryBuilder'Doctrine\ORM\QueryBuilder');
  72.         }
  73.         /*
  74.          * An orderBy DQL  part is required to avoid fetching the same row twice.
  75.          * @see http://stackoverflow.com/questions/6314879/does-limit-offset-length-require-order-by-for-pagination
  76.          * @see http://www.postgresql.org/docs/current/static/queries-limit.html
  77.          * @see http://www.sqlite.org/lang_select.html#orderby
  78.          */
  79.         $orderBy $queryBuilder->getDQLPart('orderBy');
  80.         if (empty($orderBy)) {
  81.             $rootAliases $queryBuilder->getRootAliases();
  82.             $identifierFieldNames $this->managerRegistry
  83.                 ->getManagerForClass($this->objectClass)
  84.                 ->getClassMetadata($this->objectClass)
  85.                 ->getIdentifierFieldNames();
  86.             foreach ($identifierFieldNames as $fieldName) {
  87.                 $queryBuilder->addOrderBy($rootAliases[0].'.'.$fieldName);
  88.             }
  89.         }
  90.         return $queryBuilder
  91.             ->setFirstResult($offset)
  92.             ->setMaxResults($limit)
  93.             ->getQuery()
  94.             ->getResult();
  95.     }
  96.     /**
  97.      * {@inheritDoc}
  98.      */
  99.     protected function createQueryBuilder($method, array $arguments = array())
  100.     {
  101.         $repository $this->managerRegistry
  102.             ->getManagerForClass($this->objectClass)
  103.             ->getRepository($this->objectClass);
  104.         // ORM query builders require an alias argument
  105.         $arguments = [static::ENTITY_ALIAS] + $arguments;
  106.         return call_user_func_array([$repository$method], $arguments);
  107.     }
  108. }