Пример #1
0
 /**
  * @param BaseRepository $repository
  * @param mixed $column
  * @param Builder $query
  * @param string $search
  */
 public function apply($repository, $column, $query, $search)
 {
     $name = $column->name();
     if ($repository->hasColumn($name)) {
         $query->andWhere($query->getFrom() . ".{$name} LIKE '%{$search}%'");
     } else {
         $query->andWhere("{$name} LIKE '%{$search}%'");
     }
 }
Пример #2
0
 /**
  * @param BaseRepository $repository
  * @param Builder $query
  * @param string $orderDirection
  */
 public function order($repository, $query, $orderDirection)
 {
     $name = $this->name();
     if ($repository->hasColumn($name)) {
         $query->orderBy($query->getFrom() . ".{$name} {$orderDirection}");
     } else {
         $query->orderBy("{$name} {$orderDirection}");
     }
 }
Пример #3
0
 /**
  * @param BaseRepository $repository
  * @param Builder $query
  * @param string $search
  */
 public function search($repository, $query, $search)
 {
     $name = $this->name();
     if ($repository->hasColumn($name)) {
         $query->orWhere($query->getFrom() . ".{$name} LIKE '%{$search}%'");
     } else {
         $query->orWhere("{$name} LIKE '%{$search}%'");
     }
 }
 /**
  * Добавляет ->orderBy в $builder.
  * @param \Phalcon\Mvc\Model\Query\Builder $builder
  * @param array $params
  * @return \Phalcon\Mvc\Model\Query\Builder
  */
 private static function orderBy(Builder $builder, array $params)
 {
     $model = $builder->getFrom();
     if (empty($params['orderBy'])) {
         return $builder;
     }
     $orderBy = (array) $params['orderBy'];
     foreach ($orderBy as $key => $item) {
         $order = explode('|', $item);
         $order = implode(' ', $order);
         if (!preg_match('/\\./', $item)) {
             $order = $model . '.' . $order;
         }
         $orderBy[$key] = $order;
     }
     return $builder->orderBy($orderBy);
 }
 public function applyQuery(\Phalcon\Mvc\Model\Query\Builder $builder, Query $query, ApiResource $resource)
 {
     $from = $builder->getFrom();
     $fromString = is_array($from) ? array_keys($from)[0] : $from;
     if ($query->hasFields()) {
         $builder->columns($query->getFields());
     }
     if ($query->hasOffset()) {
         $builder->offset($query->getOffset());
     }
     if ($query->hasLimit()) {
         $builder->limit($query->getLimit());
     }
     if ($query->hasConditions()) {
         $conditions = $query->getConditions();
         $andConditions = [];
         $orConditions = [];
         /** @var Condition $condition */
         foreach ($conditions as $conditionIndex => $condition) {
             if ($condition->getType() == Condition::TYPE_AND) {
                 $andConditions[] = $condition;
             } else {
                 if ($condition->getType() == Condition::TYPE_OR) {
                     $orConditions[] = $condition;
                 }
             }
         }
         $allConditions = $orConditions + $andConditions;
         /** @var Condition $condition */
         foreach ($allConditions as $conditionIndex => $condition) {
             $operator = $this->getOperator($condition->getOperator());
             if (!$operator) {
                 continue;
             }
             $parsedValues = $this->parseValues($operator, $condition->getValue());
             $format = $this->getConditionFormat($operator);
             $valuesReplacementString = $this->getValuesReplacementString($parsedValues, $conditionIndex);
             $fieldString = sprintf('[%s].[%s]', $fromString, $condition->getField());
             $conditionString = sprintf($format, $fieldString, $operator, $valuesReplacementString);
             $bindValues = $this->getBindValues($parsedValues, $conditionIndex);
             switch ($condition->getType()) {
                 case Condition::TYPE_OR:
                     $builder->orWhere($conditionString, $bindValues);
                     break;
                 case Condition::TYPE_AND:
                 default:
                     $builder->andWhere($conditionString, $bindValues);
                     break;
             }
         }
     }
     if ($query->hasExcludes()) {
         $builder->notInWhere($fromString . '.' . $resource->getModelPrimaryKey(), $query->getExcludes());
     }
     if ($query->hasSorters()) {
         $sorters = $query->getSorters();
         /** @var Sorter $sorter */
         foreach ($sorters as $sorter) {
             switch ($sorter->getDirection()) {
                 case Sorter::DESCENDING:
                     $direction = 'DESC';
                     break;
                 case Sorter::ASCENDING:
                 default:
                     $direction = 'ASC';
                     break;
             }
             $fieldString = sprintf('[%s].[%s]', $fromString, $sorter->getField());
             $builder->orderBy($fieldString . ' ' . $direction);
         }
     }
 }