Example #1
0
 /**
  * @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++;
 }