Cake\Database\Query::order PHP Method

order() public method

Fields can be passed as an array of strings, array of expression objects, a single expression or a single string. If an array is passed, keys will be used as the field itself and the value will represent the order in which such field should be ordered. When called multiple times with the same fields as key, the last order definition will prevail over the others. By default this function will append any passed argument to the list of fields to be selected, unless the second argument is set to true. ### Examples: $query->order(['title' => 'DESC', 'author_id' => 'ASC']); Produces: ORDER BY title DESC, author_id ASC $query->order(['title' => 'DESC NULLS FIRST'])->order('author_id'); Will generate: ORDER BY title DESC NULLS FIRST, author_id $expression = $query->newExpr()->add(['id % 2 = 0']); $query->order($expression)->order(['title' => 'ASC']); Will become: ORDER BY (id %2 = 0), title ASC If you need to set complex expressions as order conditions, you should use orderAsc() or orderDesc().
public order ( array | Cake\Database\ExpressionInterface | string $fields, boolean $overwrite = false )
$fields array | Cake\Database\ExpressionInterface | string fields to be added to the list
$overwrite boolean whether to reset order with field list or not
    public function order($fields, $overwrite = false)
    {
        if ($overwrite) {
            $this->_parts['order'] = null;
        }
        if (!$fields) {
            return $this;
        }
        if (!$this->_parts['order']) {
            $this->_parts['order'] = new OrderByExpression();
        }
        $this->_conjugate('order', $fields, '', []);
        return $this;
    }

Usage Example

 /**
  * @inheritDoc
  */
 public function testSelectOrderBy()
 {
     $query = new Query($this->connection);
     $result = $query->select(['id'])->from('articles')->order(['id' => 'desc'])->execute();
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $result = $query->order(['id' => 'asc'])->execute();
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $result = $query->order(['title' => 'asc'])->execute();
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $result = $query->order(['title' => 'asc'], true)->execute();
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $result = $query->order(['title' => 'asc', 'published' => 'asc'], true)->execute();
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $driver = $query->connection()->driver();
     $idField = $driver->quoteIfAutoQuote('id');
     $expression = $query->newExpr(["MOD(({$idField} + :offset), 2)"]);
     $result = $query->order([$expression, 'id' => 'desc'], true)->bind(':offset', 1, null)->execute();
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
     $result = $query->order($expression, true)->order(['id' => 'asc'])->bind(':offset', 1, null)->execute();
     $this->assertEquals(['id' => 1], $result->fetch('assoc'));
     $this->assertEquals(['id' => 3], $result->fetch('assoc'));
     $this->assertEquals(['id' => 2], $result->fetch('assoc'));
 }
All Usage Examples Of Cake\Database\Query::order