Esempio n. 1
0
 /**
  * @param \Kdyby\Doctrine\QueryBuilder $qb
  * @param array $parameters
  * @return $this
  */
 protected function addParameters(QueryBuilder $qb, array $parameters)
 {
     foreach ($parameters as $key => $value) {
         $qb->setParameter($key, $value);
     }
     return $this;
 }
Esempio n. 2
0
 /**
  * @param QueryBuilder|NativeQueryBuilder $query
  * @param array $args
  * @return array
  */
 public static function separateParameters($query, array $args)
 {
     for ($i = 0; array_key_exists($i, $args) && array_key_exists($i + 1, $args) && ($arg = $args[$i]); $i++) {
         if (!preg_match_all('~((\\:|\\?)(?P<name>[a-z0-9_]+))(?=(?:\\z|\\s|\\)))~i', $arg, $m)) {
             continue;
         }
         $repeatedArgs = [];
         foreach ($m['name'] as $l => $name) {
             if (isset($repeatedArgs[$name])) {
                 continue;
             }
             $value = $args[++$i];
             $type = NULL;
             if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
                 $type = Type::DATETIME;
             } elseif (is_array($value)) {
                 $type = Connection::PARAM_STR_ARRAY;
             }
             $query->setParameter($name, $value, $type);
             $repeatedArgs[$name] = TRUE;
             unset($args[$i]);
         }
     }
     return $args;
 }
Esempio n. 3
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++;
 }
 /**
  * @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");
         }
     }
 }