Ejemplo n.º 1
0
 /**
  * Добавляет ->limit в $builder.
  * @param \Phalcon\Mvc\Model\Query\Builder $builder
  * @param array $params
  * @return \Phalcon\Mvc\Model\Query\Builder
  */
 private static function limit(Builder $builder, array $params)
 {
     if (empty($params['limit'])) {
         return $builder;
     }
     $limit = (int) $params['limit'];
     $builder->limit($limit);
     if (empty($params['offset'])) {
         return $builder;
     }
     $offset = (int) $params['offset'];
     return $builder->offset($offset);
 }
 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);
         }
     }
 }