/**
  * Добавляет ->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);
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Apply offset and limit to the query
  * @param Builder $query
  */
 protected function applyOffset($query)
 {
     $offset = $this->di->get('request')->getQuery('start', 'int', 0);
     $limit = $this->di->get('request')->getQuery('length', 'int', 10);
     if ($limit == -1) {
         return;
     }
     $query->limit($limit, $offset);
 }
Exemplo n.º 4
0
 /**
  * Allows to query the first record that match the specified conditions
  *
  * <code>
  *
  * //What's the first robot in robots table?
  * $robot = Robots::findFirst();
  * echo "The robot name is ", $robot->name;
  *
  * //What's the first mechanical robot in robots table?
  * $robot = Robots::findFirst("type='mechanical'");
  * echo "The first mechanical robot name is ", $robot->name;
  *
  * //Get first virtual robot ordered by name
  * $robot = Robots::findFirst(array("type='virtual'", "order" => "name"));
  * echo "The first virtual robot name is ", $robot->name;
  *
  * </code>
  *
  * @param mixed $parameters
  * @return \Phalcon\Mvc\Model
  * @throws Exception
  */
 public static function findFirst($parameters = null)
 {
     if (is_array($parameters) === false) {
         $params = array();
         if (is_null($parameters) === false) {
             $params[] = $parameters;
         }
     } else {
         $params = $parameters;
     }
     //Builds a query with the passed parameters
     $builder = new Builder($params);
     $builder->from(get_called_class());
     //We only want the first record
     $builder->limit(1);
     $query = $builder->getQuery();
     $bindParams = null;
     $bindTypes = null;
     //Check for bind parameters
     if (isset($params['bind']) === true) {
         $bindParams = $params['bind'];
         if (isset($params['bindTypes']) === true) {
             $bindTypes = $params['bindTypes'];
         }
     }
     //Pass the cache options to the query
     if (isset($params['cache']) === true) {
         $query->cache($params['cache']);
     }
     //Return only the first row
     $query->setUniqueRow(true);
     //Execute the query passing the bind-params and casting-types
     return $query->execute($bindParams, $bindTypes);
 }