/**
  * @param string $sort
  * @param mixed $order Description
  * @return \Doctrine\ORM\Query\Expr\OrderBy
  */
 public function add($sort, $order = 0)
 {
     if (is_int($order)) {
         $order = $order == 0 ? self::$asc : self::$desc;
     } else {
         $order = strtoupper($order) == self::$asc ? self::$asc : self::$desc;
     }
     parent::add($sort, $order);
     return $this;
 }
 /**
  * @param array $orderBy
  * @param QueryBuilder $qb
  * @return OrderBy
  */
 protected function normalizeOrderBy(array $orderBy, QueryBuilder $qb)
 {
     $expr = new OrderBy();
     foreach ($orderBy as $field => $direction) {
         if (false === strpos($field, '.')) {
             $rootAlias = $qb->getRootAlias();
             $field = "{$rootAlias}.{$field}";
         }
         $expr->add($field, $direction);
     }
     return $expr;
 }
Beispiel #3
0
 /**
  * @param string       $entityName
  * @param integer|null $offset
  * @param integer|null $limit
  * @return int
  */
 protected function reindexSingleEntity($entityName, $offset = null, $limit = null)
 {
     /** @var EntityManager $entityManager */
     $entityManager = $this->registry->getManagerForClass($entityName);
     $entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
     $pk = $entityManager->getClassMetadata($entityName)->getIdentifier();
     $orderingsExpr = new OrderBy();
     foreach ($pk as $fieldName) {
         $orderingsExpr->add('entity.' . $fieldName);
     }
     $queryBuilder = $entityManager->getRepository($entityName)->createQueryBuilder('entity')->orderBy($orderingsExpr);
     if (null !== $offset) {
         $queryBuilder->setFirstResult($offset);
     }
     if (null !== $limit) {
         $queryBuilder->setMaxResults($limit);
     }
     $iterator = new BufferedQueryResultIterator($queryBuilder);
     $iterator->setBufferSize(static::BATCH_SIZE);
     $itemsCount = 0;
     $entities = [];
     foreach ($iterator as $entity) {
         $entities[] = $entity;
         $itemsCount++;
         if (0 == $itemsCount % static::BATCH_SIZE) {
             $this->save($entities, true);
             $entityManager->clear();
             $entities = [];
             gc_collect_cycles();
         }
     }
     if ($itemsCount % static::BATCH_SIZE > 0) {
         $this->save($entities, true);
         $entityManager->clear();
     }
     return $itemsCount;
 }