public function createGridQueryBuilder($entityClass, $params) { $qb = $this->em->createQueryBuilder(); $qb->from($entityClass, 'o'); $this->_addJoinEntity($qb, $params); $searches = array(); if (isset($params['search']['value'])) { $searches = explode(" ", trim($params['search']['value'])); for ($index = 0; $index < count($searches); $index++) { $qb->setParameter('main_search_' . $index, '%' . $searches[$index] . '%'); } } $fieldsSearch = new Expr\Andx(); $globalSearch = new Expr\Orx(); if (isset($params['columns'])) { foreach ($params['columns'] as $columns) { if ($columns['name'] && substr($columns['name'], 0, 1) != '_') { $field = $this->_getFieldName($columns['name']); $fieldAlias = str_replace(".", "_", $columns['name']); for ($index = 0; $index < count($searches); $index++) { $globalSearch->add($qb->expr()->like($field, ':main_search_' . $index)); } if ($columns['search']['value']) { $fieldsSearch->add($qb->expr()->like($field, ':' . $fieldAlias . '_search')); $qb->setParameter($fieldAlias . '_search', '%' . $columns['search']['value'] . '%'); } } } } $searchWhere = new Expr\Andx(); if (isset($fieldsSearch)) { $searchWhere->add($fieldsSearch); } if (isset($globalSearch)) { $searchWhere->add($globalSearch); } if ($searchWhere->count()) { $qb->andWhere($searchWhere); } return $qb; }
/** * @param \Doctrine\ORM\QueryBuilder $query * @return \Doctrine\ORM\QueryBuilder */ public function generateWhere(QueryBuilder $query) { $orxSearch = new Expr\Orx(); $andxIndv = new Expr\Andx(); $parameters = new ArrayCollection(); foreach ($this->getClauses() as $key => $value) { if (isset($value['indv'])) { $andxIndv->add($value['indv']['clause']); $this->createParameters($value['indv']['params'], $parameters); } if (isset($value['search'])) { $orxSearch->add($value['search']['clause']); $this->createParameters($value['search']['params'], $parameters); } } $andxIndv->add($orxSearch); $query->where($andxIndv); $p = $parameters->toArray(); foreach ($p as $param) { $query->setParameter($param->getName(), $param->getValue(), $param->getType()); } return $query; }
/** * @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 Restriction[] $restrictions * * @return mixed Expr */ protected function createExprFromRestrictions(array $restrictions) { return array_reduce($restrictions, function ($expr = null, Restriction $restriction) { if ($expr === null) { return $restriction->getRestriction(); } if ($restriction->getCondition() === FilterUtility::CONDITION_OR) { if ($expr instanceof Expr\Orx) { $expr->add($restriction->getRestriction()); } else { $expr = new Expr\Orx([$expr, $restriction->getRestriction()]); } } else { if ($expr instanceof Expr\Andx) { $expr->add($restriction->getRestriction()); } else { $expr = new Expr\Andx([$expr, $restriction->getRestriction()]); } } return $expr; }); }
/** * {@inheritdoc} */ public function addConstraint(\Doctrine\ORM\Query\Expr\Andx $constraint) { $this->constraints->add($constraint); return $this; }