/**
  * Replaces a single having clause because scalar types are not supported in doctrine paginator by default
  *
  * @param Query\Expr\Comparison $comparison
  * @param ColumnCollection      $columns
  * @param QueryBuilder          $queryBuilder
  */
 protected function replaceSingleHavingClause(Query\Expr\Comparison $comparison, ColumnCollection $columns, QueryBuilder $queryBuilder)
 {
     $source = $columns->get($comparison->getLeftExpr())->getSource();
     $param = $comparison->getRightExpr();
     $operator = $this->getOperator($comparison->getOperator());
     $expression = $queryBuilder->expr()->{$operator}($source, $param);
     $queryBuilder->andWhere($expression);
 }
 function it_adds_a_filter_on_a_field_in_the_query(QueryBuilder $qb, Expr $expr, EntityManager $em, ClassMetadata $cm, Expr\Comparison $comparison)
 {
     $qb->expr()->willReturn($expr);
     $qb->getRootAlias()->willReturn('p');
     $qb->getRootEntities()->willReturn([]);
     $qb->getEntityManager()->willReturn($em);
     $em->getClassMetadata(false)->willReturn($cm);
     $comparison->__toString()->willReturn('filterCompleteness.ratio < 100');
     $cm->getAssociationMapping('completenesses')->willReturn(['targetEntity' => 'test']);
     $expr->literal('100')->willReturn('100');
     $expr->lt(Argument::any(), '100')->willReturn($comparison);
     $this->setQueryBuilder($qb);
     $qb->leftJoin('PimCatalogBundle:Locale', Argument::any(), 'WITH', Argument::any())->willReturn($qb);
     $qb->leftJoin('PimCatalogBundle:Channel', Argument::any(), 'WITH', Argument::any())->willReturn($qb);
     $qb->leftJoin('test', Argument::any(), 'WITH', Argument::any())->willReturn($qb);
     $qb->setParameter('cLocaleCode', Argument::any())->willReturn($qb);
     $qb->setParameter('cScopeCode', Argument::any())->willReturn($qb);
     $qb->andWhere('filterCompleteness.ratio < 100')->shouldBeCalled();
     $this->addFieldFilter('completeness', '<', 100, 'en_US', 'mobile');
 }
示例#3
0
 /**
  * @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());
 }
 /**
  * Applies the given restriction to the WHERE part of the query
  *
  * @param mixed  $restriction The restriction to check.
  * @param string $condition   The condition.
  *                            Can be FilterUtility::CONDITION_OR or FilterUtility::CONDITION_AND.
  * @return bool true if a the given restriction was applied to the query builder; otherwise, false.
  */
 protected function tryApplyWhereRestriction($restriction, $condition)
 {
     if (!$restriction instanceof Expr\Comparison) {
         return false;
     }
     $expectedAlias = (string) $restriction->getLeftExpr();
     $extraSelect = null;
     foreach ($this->qb->getDQLPart('select') as $selectPart) {
         foreach ($selectPart->getParts() as $part) {
             if (preg_match("#(.*)\\s+as\\s+" . preg_quote($expectedAlias) . "#i", $part, $matches)) {
                 $extraSelect = $matches[1];
                 break;
             }
         }
     }
     if ($extraSelect === null) {
         return false;
     }
     $restriction = new Expr\Comparison($extraSelect, $restriction->getOperator(), $restriction->getRightExpr());
     $this->addRestriction($restriction, $condition);
     return true;
 }