1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\base\models\traits; |
14
|
|
|
|
15
|
|
|
use rhosocial\base\helpers\Number; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This trait is used for building entity query class for entity model. |
19
|
|
|
* |
20
|
|
|
* @version 1.0 |
21
|
|
|
* @author vistart <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait EntityQueryTrait |
24
|
|
|
{ |
25
|
|
|
use QueryTrait; |
26
|
|
|
|
27
|
|
|
public $noInitModel; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Build model without any initializations. |
31
|
|
|
*/ |
32
|
359 |
|
public function buildNoInitModel() |
33
|
|
|
{ |
34
|
359 |
|
if (empty($this->noInitModel) && is_string($this->modelClass)) { |
35
|
|
|
$modelClass = $this->modelClass; |
|
|
|
|
36
|
|
|
$this->noInitModel = $modelClass::buildNoInitModel(); |
37
|
|
|
} |
38
|
359 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Specify guid attribute. |
42
|
|
|
* @param string|array $guid |
43
|
|
|
* @param false|string $like false, 'like', 'or like', 'not like', 'or not like'. |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
16 |
|
public function guid($guid, $like = false) |
47
|
|
|
{ |
48
|
16 |
|
$model = $this->noInitModel; |
49
|
16 |
|
return $this->likeCondition((string)$guid, $model->guidAttribute, $like); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Specify id attribute. |
54
|
|
|
* @param string|integer|array $id |
55
|
|
|
* @param false|string $like false, 'like', 'or like', 'not like', 'or not like'. |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
9 |
|
public function id($id, $like = false) |
59
|
|
|
{ |
60
|
9 |
|
$model = $this->noInitModel; |
61
|
9 |
|
return $this->likeCondition($id, $model->idAttribute, $like); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Specify GUID or ID attribute. |
66
|
|
|
* Scalar parameter is acceptable only. |
67
|
|
|
* Please do not pass an array to the first parameter. |
68
|
|
|
* @param string|integer $param |
69
|
|
|
* @param bool|string $like false, 'like', 'or like', 'not like', 'or not like'. |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function guidOrId($param, $like = false) |
73
|
|
|
{ |
74
|
|
|
$model = $this->noInitModel; |
|
|
|
|
75
|
|
|
if (is_string($param) && (preg_match(Number::GUID_REGEX, $param) || strlen($param) == 16)) { |
76
|
|
|
return $this->guid($param, $like); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
return $this->id($param, $like); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Specify create time range. |
83
|
|
|
* @param string $start |
84
|
|
|
* @param string $end |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
6 |
|
public function createdAt($start = null, $end = null) |
88
|
|
|
{ |
89
|
6 |
|
$model = $this->noInitModel; |
90
|
|
|
/* @var $this yii\db\ActiveQuery */ |
91
|
6 |
|
if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) { |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
6 |
|
return static::range($this, $model->createdAtAttribute, $start, $end); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Specify order by creation time. |
99
|
|
|
* @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable. |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
3 |
|
public function orderByCreatedAt($sort = SORT_ASC) |
103
|
|
|
{ |
104
|
3 |
|
$model = $this->noInitModel; |
105
|
|
|
/* @var $this yii\db\ActiveQuery */ |
106
|
3 |
|
if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) { |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
3 |
|
return $this->addOrderBy([$model->createdAtAttribute => $sort]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Specify update time range. |
114
|
|
|
* @param string $start |
115
|
|
|
* @param string $end |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
7 |
|
public function updatedAt($start = null, $end = null) |
119
|
|
|
{ |
120
|
7 |
|
$model = $this->noInitModel; |
121
|
|
|
/* @var $this yii\db\ActiveQuery */ |
122
|
7 |
|
if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) { |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
7 |
|
return static::range($this, $model->updatedAtAttribute, $start, $end); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Specify order by update time. |
130
|
|
|
* @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable. |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
2 |
|
public function orderByUpdatedAt($sort = SORT_ASC) |
134
|
|
|
{ |
135
|
2 |
|
$model = $this->noInitModel; |
136
|
|
|
/* @var $this yii\db\ActiveQuery */ |
137
|
2 |
|
if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) { |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
2 |
|
return $this->addOrderBy([$model->updatedAtAttribute => $sort]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public static $pageAll = 'all'; |
144
|
|
|
public static $defaultPageSize = 10; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Specify page condition. |
148
|
|
|
* @param string|int $pageSize It will return all models if it is 'all', |
149
|
|
|
* or it will be regarded as sum of models. |
150
|
|
|
* @param int $currentPage The current page number if it is integer begun with 0. |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
3 |
|
public function page($pageSize = 10, $currentPage = 0) |
154
|
|
|
{ |
155
|
3 |
|
if ($pageSize === static::$pageAll) { |
156
|
2 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
/* normalize $currentPage and $currentPage */ |
159
|
1 |
|
if (!is_numeric($currentPage) || $currentPage < 0) { |
160
|
1 |
|
$currentPage = 0; |
161
|
|
|
} |
162
|
1 |
|
$currentPage = (int) $currentPage; |
163
|
1 |
|
if (!is_numeric($pageSize) || $pageSize < 1) { |
164
|
1 |
|
$pageSize = static::$defaultPageSize; |
165
|
|
|
} |
166
|
1 |
|
$pageSize = (int) $pageSize; |
167
|
1 |
|
return $this->limit($pageSize)->offset($pageSize * $currentPage); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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: