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 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; }
/** * @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; }