DboSource::order PHP Method

order() public method

Returns an ORDER BY clause as a string.
public order ( array | string $keys, string $direction = 'ASC', Model $Model = null ) : string
$keys array | string Field reference, as a key (i.e. Post.title)
$direction string Direction (ASC or DESC)
$Model Model Model reference (used to look for virtual field)
return string ORDER BY clause
    public function order($keys, $direction = 'ASC', Model $Model = null)
    {
        if (!is_array($keys)) {
            $keys = array($keys);
        }
        $keys = array_filter($keys);
        $result = array();
        while (!empty($keys)) {
            list($key, $dir) = each($keys);
            array_shift($keys);
            if (is_numeric($key)) {
                $key = $dir;
                $dir = $direction;
            }
            if (is_string($key) && strpos($key, ',') !== false && !preg_match('/\\(.+\\,.+\\)/', $key)) {
                $key = array_map('trim', explode(',', $key));
            }
            if (is_array($key)) {
                //Flatten the array
                $key = array_reverse($key, true);
                foreach ($key as $k => $v) {
                    if (is_numeric($k)) {
                        array_unshift($keys, $v);
                    } else {
                        $keys = array($k => $v) + $keys;
                    }
                }
                continue;
            } elseif (is_object($key) && isset($key->type) && $key->type === 'expression') {
                $result[] = $key->value;
                continue;
            }
            if (preg_match('/\\x20(ASC|DESC).*/i', $key, $_dir)) {
                $dir = $_dir[0];
                $key = preg_replace('/\\x20(ASC|DESC).*/i', '', $key);
            }
            $key = trim($key);
            if ($Model !== null) {
                if ($Model->isVirtualField($key)) {
                    $key = '(' . $this->_quoteFields($Model->getVirtualField($key)) . ')';
                }
                list($alias) = pluginSplit($key);
                if ($alias !== $Model->alias && is_object($Model->{$alias}) && $Model->{$alias}->isVirtualField($key)) {
                    $key = '(' . $this->_quoteFields($Model->{$alias}->getVirtualField($key)) . ')';
                }
            }
            if (strpos($key, '.')) {
                $key = preg_replace_callback('/([a-zA-Z0-9_-]{1,})\\.([a-zA-Z0-9_-]{1,})/', array(&$this, '_quoteMatchedField'), $key);
            }
            if (!preg_match('/\\s/', $key) && strpos($key, '.') === false) {
                $key = $this->name($key);
            }
            $key .= ' ' . trim($dir);
            $result[] = $key;
        }
        if (!empty($result)) {
            return ' ORDER BY ' . implode(', ', $result);
        }
        return '';
    }

Usage Example

 /**
  * testComplexSortExpression method
  *
  * @return void
  */
 public function testComplexSortExpression()
 {
     $result = $this->Dbo->order(array('(Model.field > 100) DESC', 'Model.field ASC'));
     $this->assertRegExp("/^\\s*ORDER BY\\s+\\(`Model`\\.`field`\\s+>\\s+100\\)\\s+DESC,\\s+`Model`\\.`field`\\s+ASC\\s*\$/", $result);
 }
All Usage Examples Of DboSource::order
DboSource