示例#1
0
 /**
  * Add a sort rule for this query
  *
  * If called without a specific column, the repository's defaul sort rules will be applied.
  * This notifies the repository about each column being required as filter column.
  *
  * @param   string  $field          The name of the column by which to sort the query's result
  * @param   string  $direction      The direction to use when sorting (asc or desc, default is asc)
  * @param   bool    $ignoreDefault  Whether to ignore any default sort rules if $field is given
  *
  * @return  $this
  */
 public function order($field = null, $direction = null, $ignoreDefault = false)
 {
     $sortRules = $this->repository->getSortRules();
     if ($field === null) {
         // Use first available sort rule as default
         if (empty($sortRules)) {
             // Return early in case of no sort defaults and no given $field
             return $this;
         }
         $sortColumns = reset($sortRules);
         if (!array_key_exists('columns', $sortColumns)) {
             $sortColumns['columns'] = array(key($sortRules));
         }
         if ($direction !== null || !array_key_exists('order', $sortColumns)) {
             $sortColumns['order'] = $direction ?: static::SORT_ASC;
         }
     } else {
         $alias = $this->repository->reassembleQueryColumnAlias($this->target, $field) ?: $field;
         if (!$ignoreDefault && array_key_exists($alias, $sortRules)) {
             $sortColumns = $sortRules[$alias];
             if (!array_key_exists('columns', $sortColumns)) {
                 $sortColumns['columns'] = array($alias);
             }
             if ($direction !== null || !array_key_exists('order', $sortColumns)) {
                 $sortColumns['order'] = $direction ?: static::SORT_ASC;
             }
         } else {
             $sortColumns = array('columns' => array($alias), 'order' => $direction);
         }
     }
     $baseDirection = strtoupper($sortColumns['order']) === static::SORT_DESC ? static::SORT_DESC : static::SORT_ASC;
     foreach ($sortColumns['columns'] as $column) {
         list($column, $specificDirection) = $this->splitOrder($column);
         if ($this->hasLimit() && $this->repository->providesValueConversion($this->target, $column)) {
             Logger::debug('Cannot order by column "%s" in repository "%s". The query is' . ' limited and applies value conversion rules on the column', $column, $this->repository->getName());
             continue;
         }
         try {
             $this->query->order($this->repository->requireFilterColumn($this->target, $column, $this), $specificDirection ?: $baseDirection);
         } catch (QueryException $_) {
             Logger::info('Cannot order by column "%s" in repository "%s"', $column, $this->repository->getName());
         }
     }
     return $this;
 }
示例#2
0
 /**
  * Return the sort rules being applied on this query
  *
  * @return  array
  */
 public function getSortRules()
 {
     return $this->repository->getSortRules();
 }