|
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
|
|
|
/** |
|
16
|
|
|
* This trait is used for building entity query class for entity model. |
|
17
|
|
|
* |
|
18
|
|
|
* @version 1.0 |
|
19
|
|
|
* @author vistart <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
trait EntityQueryTrait |
|
22
|
|
|
{ |
|
23
|
|
|
use QueryTrait; |
|
24
|
|
|
|
|
25
|
|
|
public $noInitModel; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Build model without any initializations. |
|
29
|
|
|
*/ |
|
30
|
289 |
|
public function buildNoInitModel() |
|
31
|
|
|
{ |
|
32
|
289 |
|
if (empty($this->noInitModel) && is_string($this->modelClass)) { |
|
33
|
|
|
$modelClass = $this->modelClass; |
|
|
|
|
|
|
34
|
|
|
$this->noInitModel = $modelClass::buildNoInitModel(); |
|
35
|
|
|
} |
|
36
|
289 |
|
} |
|
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
|
12 |
|
public function guid($guid, $like = false) |
|
45
|
|
|
{ |
|
46
|
12 |
|
$model = $this->noInitModel; |
|
47
|
12 |
|
return $this->likeCondition((string)$guid, $model->guidAttribute, $like); |
|
48
|
|
|
} |
|
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) |
|
57
|
|
|
{ |
|
58
|
9 |
|
$model = $this->noInitModel; |
|
59
|
9 |
|
return $this->likeCondition($id, $model->idAttribute, $like); |
|
60
|
|
|
} |
|
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) |
|
69
|
|
|
{ |
|
70
|
5 |
|
$model = $this->noInitModel; |
|
71
|
|
|
/* @var $model static */ |
|
72
|
5 |
|
if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) { |
|
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
5 |
|
return static::range($this, $model->createdAtAttribute, $start, $end); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Specify order by creation time. |
|
80
|
|
|
* @param string $sort only 'ASC' and 'DESC' are acceptable. |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function orderByCreatedAt($sort = 'ASC') |
|
84
|
|
|
{ |
|
85
|
|
|
$model = $this->noInitModel; |
|
86
|
|
|
/* @var $model static */ |
|
87
|
|
|
if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) { |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
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) |
|
100
|
|
|
{ |
|
101
|
7 |
|
$model = $this->noInitModel; |
|
102
|
|
|
/* @var $model static */ |
|
103
|
7 |
|
if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) { |
|
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
7 |
|
return static::range($this, $model->updatedAtAttribute, $start, $end); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Specify order by update time. |
|
111
|
|
|
* @param string $sort only 'ASC' and 'DESC' are acceptable. |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function orderByUpdatedAt($sort = 'ASC') |
|
115
|
|
|
{ |
|
116
|
|
|
$model = $this->noInitModel; |
|
117
|
|
|
/* @var $model static */ |
|
118
|
|
|
if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) { |
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
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) |
|
135
|
|
|
{ |
|
136
|
3 |
|
if ($pageSize === static::$pageAll) { |
|
137
|
2 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
/* normalize $currentPage and $currentPage */ |
|
140
|
1 |
|
if (!is_numeric($currentPage) || $currentPage < 0) { |
|
141
|
1 |
|
$currentPage = 0; |
|
142
|
|
|
} |
|
143
|
1 |
|
$currentPage = (int) $currentPage; |
|
144
|
1 |
|
if (!is_numeric($pageSize) || $pageSize < 1) { |
|
145
|
1 |
|
$pageSize = static::$defaultPageSize; |
|
146
|
|
|
} |
|
147
|
1 |
|
$pageSize = (int) $pageSize; |
|
148
|
1 |
|
return $this->limit($pageSize)->offset($pageSize * $currentPage); |
|
|
|
|
|
|
149
|
|
|
} |
|
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: