/** * @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; }
/** * @group DDC-1686 */ public function testExpressionGetter() { // Andx $andx = new Expr\Andx(array('1 = 1', '2 = 2')); $this->assertEquals(array('1 = 1', '2 = 2'), $andx->getParts()); // Comparison $comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar'); $this->assertEquals('foo', $comparison->getLeftExpr()); $this->assertEquals('bar', $comparison->getRightExpr()); $this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator()); // From $from = new Expr\From('Foo', 'f', 'f.id'); $this->assertEquals('f', $from->getAlias()); $this->assertEquals('Foo', $from->getFrom()); $this->assertEquals('f.id', $from->getIndexBy()); // Func $func = new Expr\Func('MAX', array('f.id')); $this->assertEquals('MAX', $func->getName()); $this->assertEquals(array('f.id'), $func->getArguments()); // GroupBy $group = new Expr\GroupBy(array('foo DESC', 'bar ASC')); $this->assertEquals(array('foo DESC', 'bar ASC'), $group->getParts()); // Join $join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id'); $this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType()); $this->assertEquals(Expr\Join::ON, $join->getConditionType()); $this->assertEquals('b.bar_id = 1', $join->getCondition()); $this->assertEquals('b.bar_id', $join->getIndexBy()); $this->assertEquals('f.bar', $join->getJoin()); $this->assertEquals('b', $join->getAlias()); // Literal $literal = new Expr\Literal(array('foo')); $this->assertEquals(array('foo'), $literal->getParts()); // Math $math = new Expr\Math(10, '+', 20); $this->assertEquals(10, $math->getLeftExpr()); $this->assertEquals(20, $math->getRightExpr()); $this->assertEquals('+', $math->getOperator()); // OrderBy $order = new Expr\OrderBy('foo', 'DESC'); $this->assertEquals(array('foo DESC'), $order->getParts()); // Andx $orx = new Expr\Orx(array('foo = 1', 'bar = 2')); $this->assertEquals(array('foo = 1', 'bar = 2'), $orx->getParts()); // Select $select = new Expr\Select(array('foo', 'bar')); $this->assertEquals(array('foo', 'bar'), $select->getParts()); }
/** * @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; }
/** * @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; }
/** * Gets the parts from an OrderBy expression. * * @param OrderBy $orderBy * * @return string[] */ public static function getOrderByParts(OrderBy $orderBy) : array { static $partsProperty = null; static $initialized = false; if (!$initialized && !method_exists(OrderBy::class, 'getParts')) { $partsProperty = new \ReflectionProperty(OrderBy::class, '_parts'); $partsProperty->setAccessible(true); $initialized = true; } return null === $partsProperty ? $orderBy->getParts() : $partsProperty->getValue($orderBy); }