/** * @param \Kdyby\Doctrine\QueryBuilder|\Kdyby\Doctrine\NativeQueryBuilder $qb * @param \Carrooi\NoGrid\Condition $condition * @throws \Carrooi\NoGrid\NotImplementedException */ public static function makeWhere($qb, Condition $condition) { if (!$qb instanceof QueryBuilder && !$qb instanceof NativeQueryBuilder) { throw new InvalidArgumentException(); } $column = $condition->getColumn(); $value = $condition->getValue(); if (!Strings::contains($column, '.')) { $column = current($qb->getRootAliases()) . '.' . $column; } $parameter = ':grid' . self::$parametersCount; $options = $condition->getOptions(); $lower = isset($options[Condition::CASE_INSENSITIVE]) && $options[Condition::CASE_INSENSITIVE]; if ($lower) { $column = 'lower(' . $column . ')'; $parameter = 'lower(' . $parameter . ')'; } if ($condition->getType() === Condition::SAME) { $qb->andWhere($column . ' = ' . $parameter); } elseif ($condition->getType() === Condition::NOT_SAME) { $qb->andWhere($column . ' != ' . $parameter); } elseif ($condition->getType() === Condition::IS_NULL) { $qb->andWhere($column . ' IS NULL'); } elseif ($condition->getType() === Condition::IS_NOT_NULL) { $qb->andWhere($column . ' IS NOT NULL'); } elseif ($condition->getType() === Condition::LIKE) { $qb->andWhere($column . ' LIKE ' . $parameter); } else { throw new NotImplementedException('Filtering condition is not implemented.'); } if (!in_array($condition->getType(), [Condition::IS_NULL, Condition::IS_NOT_NULL])) { $qb->setParameter('grid' . self::$parametersCount, $value); } self::$parametersCount++; }
/** * @param string $filter_str * @param QueryBuilder $query * @param string $column */ public function createStringCondition($filter_str, $query, $column) { if ($filter_str !== null) { if (preg_match('/^%.+%$/', $filter_str)) { $query->andWhere($query->expr()->like($column, "'" . $filter_str . "'")); } else { $query->andWhere($query->expr()->eq($column, "'" . $filter_str . "'")); } } }
/** * @param EntityManager * @param ISortableScope * @param QueryBuilder */ private function addScope(EntityManager $em, ISortableScope $sortable, QueryBuilder $qb) { $meta = $em->getClassMetadata(get_class($sortable)); $rc = new \ReflectionClass($sortable); foreach ($sortable->getSortableScope() as $field) { if ($meta->hasField($field) || $meta->hasAssociation($field)) { $rp = $rc->getProperty($field); $rp->setAccessible(TRUE); $qb->andWhere($qb->expr()->eq('e.' . $field, ':p_' . $field)); $qb->setParameter('p_' . $field, $rp->getValue($sortable)); } elseif ($meta->discriminatorColumn['name'] === $field) { if (($type = array_search(get_class($sortable), $meta->discriminatorMap)) === FALSE) { $type = get_class($sortable); } $qb->andWhere('e INSTANCE OF :discr_type')->setParameter('discr_type', $type); } else { throw new InvalidScopeException("Scope field {$field} is neither field, association nor discriminator"); } } }