|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\queries; |
|
10
|
|
|
|
|
11
|
|
|
use yii\db\Connection; |
|
12
|
|
|
use yii\db\Expression; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @property string|array|Expression $orderBy |
|
16
|
|
|
* |
|
17
|
|
|
* @author Flipbox Factory <[email protected]> |
|
18
|
|
|
* @since 2.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
trait OrderByTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool Whether results should be returned in the order specified by [[domain]]. |
|
24
|
|
|
*/ |
|
25
|
|
|
public $fixedOrder = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Sets the ORDER BY part of the query. |
|
29
|
|
|
* @param string|array|Expression $columns the columns (and the directions) to be ordered by. |
|
30
|
|
|
* Columns can be specified in either a string (e.g. `"id ASC, name DESC"`) or an array |
|
31
|
|
|
* (e.g. `['id' => SORT_ASC, 'name' => SORT_DESC]`). |
|
32
|
|
|
* |
|
33
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
|
34
|
|
|
* (which means the column contains a DB expression). |
|
35
|
|
|
* |
|
36
|
|
|
* Note that if your order-by is an expression containing commas, you should always use an array |
|
37
|
|
|
* to represent the order-by information. Otherwise, the method will not be able to correctly determine |
|
38
|
|
|
* the order-by columns. |
|
39
|
|
|
* |
|
40
|
|
|
* Since version 2.0.7, an [[Expression]] object can be passed to specify the ORDER BY part explicitly in plain SQL. |
|
41
|
|
|
* @return $this the query object itself |
|
42
|
|
|
* @see addOrderBy() |
|
43
|
|
|
*/ |
|
44
|
|
|
abstract public function orderBy($columns); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Applies the 'fixedOrder' and 'orderBy' params to the query being prepared. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Connection|null $db The database connection used to generate the SQL statement. |
|
50
|
|
|
* If this parameter is not given, the `db` application component will be used. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function applyOrderByParams(Connection $db) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($this->orderBy === null) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Any other empty value means we should set it |
|
59
|
|
|
if (empty($this->orderBy)) { |
|
60
|
|
|
$this->applyEmptyOrderByParams($db); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->orderBy($this->orderBy); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @noinspection PhpUnusedParameterInspection |
|
68
|
|
|
* |
|
69
|
|
|
* @param Connection $db |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function applyEmptyOrderByParams(Connection $db) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->orderBy = ['dateCreated' => SORT_DESC]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|