1 | <?php |
||
21 | trait EntityQueryTrait |
||
22 | { |
||
23 | use QueryTrait; |
||
24 | |||
25 | public $noInitModel; |
||
26 | |||
27 | /** |
||
28 | * Build model without any initializations. |
||
29 | */ |
||
30 | 295 | public function buildNoInitModel() |
|
31 | { |
||
32 | 295 | if (empty($this->noInitModel) && is_string($this->modelClass)) { |
|
33 | $modelClass = $this->modelClass; |
||
|
|||
34 | $this->noInitModel = $modelClass::buildNoInitModel(); |
||
35 | } |
||
36 | 295 | } |
|
37 | |||
38 | /** |
||
39 | * Specify guid attribute. |
||
40 | * @param string|array $guid |
||
41 | * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'. |
||
42 | * @return $this |
||
43 | */ |
||
44 | 14 | public function guid($guid, $like = false) |
|
49 | |||
50 | /** |
||
51 | * Specify id attribute. |
||
52 | * @param string|integer|array $id |
||
53 | * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'. |
||
54 | * @return $this |
||
55 | */ |
||
56 | 9 | public function id($id, $like = false) |
|
61 | |||
62 | /** |
||
63 | * Specify create time range. |
||
64 | * @param string $start |
||
65 | * @param string $end |
||
66 | * @return $this |
||
67 | */ |
||
68 | 5 | public function createdAt($start = null, $end = null) |
|
77 | |||
78 | /** |
||
79 | * Specify order by creation time. |
||
80 | * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable. |
||
81 | * @return $this |
||
82 | */ |
||
83 | 2 | public function orderByCreatedAt($sort = SORT_ASC) |
|
84 | { |
||
85 | 2 | $model = $this->noInitModel; |
|
86 | /* @var $this yii\db\ActiveQuery */ |
||
87 | 2 | if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) { |
|
88 | return $this; |
||
89 | } |
||
90 | 2 | return $this->addOrderBy([$model->createdAtAttribute => $sort]); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Specify update time range. |
||
95 | * @param string $start |
||
96 | * @param string $end |
||
97 | * @return $this |
||
98 | */ |
||
99 | 7 | public function updatedAt($start = null, $end = null) |
|
108 | |||
109 | /** |
||
110 | * Specify order by update time. |
||
111 | * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable. |
||
112 | * @return $this |
||
113 | */ |
||
114 | 2 | public function orderByUpdatedAt($sort = SORT_ASC) |
|
115 | { |
||
116 | 2 | $model = $this->noInitModel; |
|
117 | /* @var $this yii\db\ActiveQuery */ |
||
118 | 2 | if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) { |
|
119 | return $this; |
||
120 | } |
||
121 | 2 | return $this->addOrderBy([$model->updatedAtAttribute => $sort]); |
|
122 | } |
||
123 | |||
124 | public static $pageAll = 'all'; |
||
125 | public static $defaultPageSize = 10; |
||
126 | |||
127 | /** |
||
128 | * Specify page condition. |
||
129 | * @param string|int $pageSize It will return all models if it is 'all', |
||
130 | * or it will be regarded as sum of models. |
||
131 | * @param int $currentPage The current page number if it is integer begun with 0. |
||
132 | * @return $this |
||
133 | */ |
||
134 | 3 | public function page($pageSize = 10, $currentPage = 0) |
|
150 | } |
||
151 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: