|
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\ember\services\traits\queries; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use flipbox\ember\exceptions\NotFoundException; |
|
13
|
|
|
use flipbox\ember\helpers\RecordHelper; |
|
14
|
|
|
use yii\base\BaseObject; |
|
15
|
|
|
use yii\caching\Dependency; |
|
16
|
|
|
use yii\db\Connection; |
|
17
|
|
|
use yii\db\QueryInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A set of robust methods commonly used to retrieve data from the attached database. An optional |
|
21
|
|
|
* cache layer can be applied to circumvent heavy queries. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Flipbox Factory <[email protected]> |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
trait Accessor |
|
27
|
|
|
{ |
|
28
|
|
|
/******************************************* |
|
29
|
|
|
* QUERY |
|
30
|
|
|
*******************************************/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $config |
|
34
|
|
|
* @return \yii\db\ActiveQuery |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract public function getQuery($config = []): QueryInterface; |
|
37
|
|
|
|
|
38
|
|
|
/******************************************* |
|
39
|
|
|
* CACHE |
|
40
|
|
|
*******************************************/ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return int|null |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static function cacheDuration() |
|
46
|
|
|
{ |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return null|Dependency |
|
52
|
|
|
*/ |
|
53
|
|
|
protected static function cacheDependency() |
|
54
|
|
|
{ |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Connection |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static function getDb(): Connection |
|
62
|
|
|
{ |
|
63
|
|
|
return Craft::$app->getDb(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/******************************************* |
|
67
|
|
|
* FIND / GET |
|
68
|
|
|
*******************************************/ |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array[] |
|
72
|
|
|
*/ |
|
73
|
|
|
public function findAll() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->findAllByCondition(null); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $identifier |
|
80
|
|
|
* @return mixed|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function find($identifier) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->findByCondition($identifier); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $identifier |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
* @throws NotFoundException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function get($identifier) |
|
93
|
|
|
{ |
|
94
|
|
|
if (null === ($object = $this->find($identifier))) { |
|
95
|
|
|
$this->notFoundException(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $object; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/******************************************* |
|
103
|
|
|
* ONE CONDITION |
|
104
|
|
|
*******************************************/ |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param $condition |
|
108
|
|
|
* @return mixed|null |
|
109
|
|
|
*/ |
|
110
|
|
|
public function findByCondition($condition) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->findByCriteria( |
|
113
|
|
|
RecordHelper::conditionToCriteria($condition) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $condition |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
* @throws NotFoundException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getByCondition($condition) |
|
123
|
|
|
{ |
|
124
|
|
|
if (null === ($object = $this->findByCondition($condition))) { |
|
125
|
|
|
$this->notFoundException(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $object; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/******************************************* |
|
133
|
|
|
* ONE CRITERIA |
|
134
|
|
|
*******************************************/ |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param $criteria |
|
138
|
|
|
* @return mixed|null |
|
139
|
|
|
*/ |
|
140
|
|
|
public function findByCriteria($criteria) |
|
141
|
|
|
{ |
|
142
|
|
|
$object = $this->queryOne( |
|
143
|
|
|
$this->getQuery($criteria) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
return $object; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $criteria |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
* @throws NotFoundException |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getByCriteria($criteria) |
|
155
|
|
|
{ |
|
156
|
|
|
if (null === ($record = $this->findByCriteria($criteria))) { |
|
157
|
|
|
$this->notFoundException(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $record; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/******************************************* |
|
165
|
|
|
* ALL CONDITION |
|
166
|
|
|
*******************************************/ |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param array $condition |
|
170
|
|
|
* @return BaseObject[] |
|
171
|
|
|
*/ |
|
172
|
|
|
public function findAllByCondition($condition = []): array |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->findAllByCriteria( |
|
175
|
|
|
RecordHelper::conditionToCriteria($condition) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param array $condition |
|
181
|
|
|
* @return BaseObject[] |
|
182
|
|
|
* @throws NotFoundException |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getAllByCondition($condition = []): array |
|
185
|
|
|
{ |
|
186
|
|
|
$records = $this->findAllByCondition($condition); |
|
187
|
|
|
if (empty($records)) { |
|
188
|
|
|
$this->notFoundException(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $records; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/******************************************* |
|
195
|
|
|
* ALL CRITERIA |
|
196
|
|
|
*******************************************/ |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param array $criteria |
|
200
|
|
|
* @return BaseObject[] |
|
201
|
|
|
*/ |
|
202
|
|
|
public function findAllByCriteria($criteria = []): array |
|
203
|
|
|
{ |
|
204
|
|
|
$records = $this->queryAll( |
|
205
|
|
|
$this->getQuery($criteria) |
|
206
|
|
|
); |
|
207
|
|
|
|
|
208
|
|
|
return $records; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param array $criteria |
|
213
|
|
|
* @return BaseObject[] |
|
214
|
|
|
* @throws NotFoundException |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getAllByCriteria($criteria = []): array |
|
217
|
|
|
{ |
|
218
|
|
|
$records = $this->findAllByCriteria($criteria); |
|
219
|
|
|
if (empty($records)) { |
|
220
|
|
|
$this->notFoundException(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $records; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/******************************************* |
|
228
|
|
|
* CACHE |
|
229
|
|
|
*******************************************/ |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param QueryInterface $query |
|
233
|
|
|
* @return mixed|null |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function queryOne(QueryInterface $query) |
|
236
|
|
|
{ |
|
237
|
|
|
$db = static::getDb(); |
|
238
|
|
|
|
|
239
|
|
|
try { |
|
240
|
|
|
if (false === ($cacheDuration = static::cacheDuration())) { |
|
241
|
|
|
return $query->one($db); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$result = $db->cache(function ($db) use ($query) { |
|
245
|
|
|
return $query->one($db); |
|
246
|
|
|
}, $cacheDuration, static::cacheDependency()); |
|
|
|
|
|
|
247
|
|
|
} catch (\Exception $e) { |
|
248
|
|
|
return null; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $result; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @param QueryInterface $query |
|
256
|
|
|
* @return mixed[] |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function queryAll(QueryInterface $query) |
|
259
|
|
|
{ |
|
260
|
|
|
$db = static::getDb(); |
|
261
|
|
|
|
|
262
|
|
|
try { |
|
263
|
|
|
if (false === ($cacheDuration = static::cacheDuration())) { |
|
264
|
|
|
return $query->all($db); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$results = $db->cache(function ($db) use ($query) { |
|
268
|
|
|
return $query->all($db); |
|
269
|
|
|
}, $cacheDuration, static::cacheDependency()); |
|
|
|
|
|
|
270
|
|
|
} catch (\Exception $e) { |
|
271
|
|
|
return []; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return $results; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/******************************************* |
|
278
|
|
|
* EXCEPTIONS |
|
279
|
|
|
*******************************************/ |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @throws NotFoundException |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function notFoundException() |
|
285
|
|
|
{ |
|
286
|
|
|
throw new NotFoundException( |
|
287
|
|
|
sprintf( |
|
288
|
|
|
"Results not found." |
|
289
|
|
|
) |
|
290
|
|
|
); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: