|
|
@@ -16,6 +16,7 @@ namespace Cake\Database;
|
|
|
|
|
|
use Cake\Database\Exception;
|
|
|
use Cake\Database\Expression\OrderByExpression;
|
|
|
+use Cake\Database\Expression\OrderClauseExpression;
|
|
|
use Cake\Database\Expression\QueryExpression;
|
|
|
use Cake\Database\Expression\ValuesExpression;
|
|
|
use Cake\Database\Statement\CallbackStatement;
|
|
|
@@ -926,6 +927,9 @@ class Query implements ExpressionInterface, IteratorAggregate
|
|
|
*
|
|
|
* ``ORDER BY (id %2 = 0), title ASC``
|
|
|
*
|
|
|
+ * If you need to set complex expressions as order conditions, you
|
|
|
+ * should use `orderAsc()` or `orderDesc()`.
|
|
|
+ *
|
|
|
* @param array|\Cake\Database\ExpressionInterface|string $fields fields to be added to the list
|
|
|
* @param bool $overwrite whether to reset order with field list or not
|
|
|
* @return $this
|
|
|
@@ -941,13 +945,65 @@ class Query implements ExpressionInterface, IteratorAggregate
|
|
|
}
|
|
|
|
|
|
if (!$this->_parts['order']) {
|
|
|
- $this->_parts['order'] = new OrderByExpression;
|
|
|
+ $this->_parts['order'] = new OrderByExpression();
|
|
|
}
|
|
|
$this->_conjugate('order', $fields, '', []);
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Add an ORDER BY clause with an ASC direction.
|
|
|
+ *
|
|
|
+ * This method allows you to set complex expressions
|
|
|
+ * as order conditions unlike order()
|
|
|
+ *
|
|
|
+ * @param string|\Cake\Database\QueryExpression $field The field to order on.
|
|
|
+ * @param bool $overwrite Whether or not to reset the order clauses.
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function orderAsc($field, $overwrite = false)
|
|
|
+ {
|
|
|
+ if ($overwrite) {
|
|
|
+ $this->_parts['order'] = null;
|
|
|
+ }
|
|
|
+ if (!$field) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$this->_parts['order']) {
|
|
|
+ $this->_parts['order'] = new OrderByExpression();
|
|
|
+ }
|
|
|
+ $this->_parts['order']->add(new OrderClauseExpression($field, 'ASC'));
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add an ORDER BY clause with an ASC direction.
|
|
|
+ *
|
|
|
+ * This method allows you to set complex expressions
|
|
|
+ * as order conditions unlike order()
|
|
|
+ *
|
|
|
+ * @param string|\Cake\Database\QueryExpression $field The field to order on.
|
|
|
+ * @param bool $overwrite Whether or not to reset the order clauses.
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function orderDesc($field, $overwrite = false)
|
|
|
+ {
|
|
|
+ if ($overwrite) {
|
|
|
+ $this->_parts['order'] = null;
|
|
|
+ }
|
|
|
+ if (!$field) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$this->_parts['order']) {
|
|
|
+ $this->_parts['order'] = new OrderByExpression();
|
|
|
+ }
|
|
|
+ $this->_parts['order']->add(new OrderClauseExpression($field, 'DESC'));
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Adds a single or multiple fields to be used in the GROUP BY clause for this query.
|
|
|
* Fields can be passed as an array of strings, array of expression
|
|
|
* objects, a single expression or a single string.
|