1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link http://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license http://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This trait is used for building entity query class for entity model. |
17
|
|
|
* |
18
|
|
|
* @version 2.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
|
62 |
|
public function buildNoInitModel() |
31
|
|
|
{ |
32
|
62 |
|
if (empty($this->noInitModel) && is_string($this->modelClass)) { |
33
|
|
|
$modelClass = $this->modelClass; |
|
|
|
|
34
|
|
|
$this->noInitModel = $modelClass::buildNoInitModel(); |
35
|
|
|
} |
36
|
62 |
|
} |
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
|
8 |
|
public function guid($guid, $like = false) |
45
|
|
|
{ |
46
|
8 |
|
$model = $this->noInitModel; |
47
|
8 |
|
return $this->likeCondition($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
|
12 |
|
public function id($id, $like = false) |
57
|
|
|
{ |
58
|
12 |
|
$model = $this->noInitModel; |
59
|
12 |
|
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
|
1 |
|
public function createdAt($start = null, $end = null) |
69
|
|
|
{ |
70
|
1 |
|
$model = $this->noInitModel; |
71
|
1 |
|
if (!is_string($model->createdAtAttribute)) { |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
1 |
|
return static::range($this, $model->createdAtAttribute, $start, $end); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Specify update time range. |
79
|
|
|
* @param string $start |
80
|
|
|
* @param string $end |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
1 |
|
public function updatedAt($start = null, $end = null) |
84
|
|
|
{ |
85
|
1 |
|
$model = $this->noInitModel; |
86
|
1 |
|
if (!is_string($model->updatedAtAttribute)) { |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
1 |
|
return static::range($this, $model->updatedAtAttribute, $start, $end); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static $pageAll = 'all'; |
93
|
|
|
public static $defaultPageSize = 10; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Specify page condition. |
97
|
|
|
* @param string|int $pageSize It will return all models if it is 'all', |
98
|
|
|
* or it will be regarded as sum of models. |
99
|
|
|
* @param int $currentPage The current page number if it is integer begun with 0. |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function page($pageSize = 10, $currentPage = 0) |
103
|
|
|
{ |
104
|
|
|
if ($pageSize === static::$pageAll) { |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/* normalize $currentPage and $currentPage */ |
109
|
|
|
if (!is_numeric($currentPage) || $currentPage < 0) { |
110
|
|
|
$currentPage = 0; |
111
|
|
|
} |
112
|
|
|
$currentPage = (int) $currentPage; |
113
|
|
|
if (!is_numeric($pageSize) || $pageSize < 1) { |
114
|
|
|
$pageSize = static::$defaultPageSize; |
115
|
|
|
} |
116
|
|
|
$pageSize = (int) $pageSize; |
117
|
|
|
return $this->limit($pageSize)->offset($pageSize * $currentPage); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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: