示例#1
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}");
     }
 }
 /**
  * Добавляет ->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);
         }
     }
 }
示例#4
0
文件: Count.php 项目: argentum88/phad
 /**
  * @param BaseRepository $repository
  * @param Builder $query
  * @param string $orderDirection
  */
 public function order($repository, $query, $orderDirection)
 {
     $name = $this->name();
     $query->orderBy("count({$name}.id) {$orderDirection}");
 }
示例#5
0
 public function getResponse()
 {
     $builder = new PQueryBuilder(['builder' => $this->builder, 'limit' => 1, 'page' => 1]);
     $total = $builder->getPaginate();
     $this->bind('global_search', function ($column, $search) {
         $this->builder->orWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
     });
     $this->bind('column_search', function ($column, $search) {
         $this->builder->andWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
     });
     $this->bind('order', function ($order) {
         if (!empty($order)) {
             $this->builder->orderBy(implode(', ', $order));
         }
     });
     $builder = new PQueryBuilder(['builder' => $this->builder, 'limit' => $this->parser->getLimit(), 'page' => $this->parser->getPage()]);
     /* @var $filtered \Phalcon\Mvc\Model\Resultset  */
     $filtered = $builder->getPaginate();
     /* @var $metadata \Phalcon\Mvc\Model\MetaData  */
     $metadata = \Phalcon\Di::getDefault()->get('modelsMetadata');
     $item = $filtered->items->getFirst();
     if ($item instanceof \Phalcon\Mvc\Model) {
         $filtered->items->rewind();
         $columnMap = $metadata->getAttributes($item);
         $columnMap = array_combine($columnMap, $columnMap);
         $extractMethods = function ($item) {
             $reflection = new \ReflectionClass($item);
             $itemMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
             $itemMethods = array_map(function (\ReflectionMethod $reflectionMethod) {
                 return $reflectionMethod->getName();
             }, $itemMethods);
             return array_combine($itemMethods, $itemMethods);
         };
         // if use array_diff we can catch error, because $this->originalColumns can have array item
         $attributes = $methods = [];
         foreach ($this->originalColumns as $itemColumn) {
             $itemData = [];
             if (is_string($itemColumn)) {
                 // check that it is item attribute
                 if (isset($columnMap[$itemColumn])) {
                     $attributes[] = $itemColumn;
                 }
             } elseif (is_array($itemColumn)) {
                 /**
                  * Possible variants
                  * itemColumn => [methodName => [param1, param2]] - method with parameters
                  * itemColumn => methodName] - method without parameters
                  * 
                  */
                 $columnName = array_keys($itemColumn)[0];
                 $methodData = $itemColumn[$columnName];
                 if (!isset($columnMap[$columnName])) {
                     // undefined columnName
                     //continue;
                 }
                 $parameters = null;
                 if (is_array($methodData)) {
                     $methodName = array_keys($methodData)[0];
                     $parameters = $methodData[$methodName];
                 } else {
                     $methodName = $methodData;
                 }
                 // check that it is existed method
                 if (empty($itemMethods)) {
                     $itemMethods = $extractMethods($item);
                 }
                 if (isset($itemMethods[$methodName])) {
                     $methods[$columnName] = compact('methodName', 'parameters');
                 }
             }
         }
         $data = [];
         foreach ($filtered->items as $item) {
             $itemData = [];
             foreach ($attributes as $attr) {
                 $itemData[$attr] = $item->readAttribute($attr);
             }
             foreach ($methods as $columnName => $method) {
                 $parameters = !empty($method['parameters']) ? $method['parameters'] : [];
                 $itemData[$columnName] = call_user_func_array([$item, $method['methodName']], $parameters);
             }
             $data[] = $itemData;
         }
     } else {
         $data = $filtered->items->toArray();
     }
     return $this->formResponse(['total' => $total->total_items, 'filtered' => $filtered->total_items, 'data' => $data]);
 }
示例#6
0
 /**
  * Apply sorting data on array.
  *
  * @param Builder $source Data.
  *
  * @return array|void
  */
 protected function _applySorting(Builder $source)
 {
     $sort = $this->_getParam('sort');
     $direction = $this->_getParam('direction', 'DESC');
     // Additional checks.
     if (!$sort || $direction != 'DESC' && $direction != 'ASC') {
         return;
     }
     $source->orderBy(sprintf('%s %s', $sort, $direction));
 }