|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
|
6
|
|
|
* @package yii2-hiart |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\hiart\rest\QueryBuilder; |
|
14
|
|
|
use yii\db\ActiveQueryInterface; |
|
15
|
|
|
use yii\db\ActiveQueryTrait; |
|
16
|
|
|
use yii\db\ActiveRelationTrait; |
|
17
|
|
|
|
|
18
|
|
|
class ActiveQuery extends Query implements ActiveQueryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use ActiveQueryTrait; |
|
21
|
|
|
use ActiveRelationTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @event Event an event that is triggered when the query is initialized via [[init()]]. |
|
25
|
|
|
*/ |
|
26
|
|
|
const EVENT_INIT = 'init'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array|null a list of relations that this query should be joined with |
|
30
|
|
|
*/ |
|
31
|
|
|
public $joinWith = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor. |
|
35
|
|
|
* @param string $modelClass the model class associated with this query |
|
36
|
|
|
* @param array $config configurations to be applied to the newly created query object |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function __construct($modelClass, $config = []) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->modelClass = $modelClass; |
|
41
|
|
|
|
|
42
|
2 |
|
parent::__construct($config); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Initializes the object. |
|
47
|
|
|
* This method is called at the end of the constructor. The default implementation will trigger |
|
48
|
|
|
* an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end |
|
49
|
|
|
* to ensure triggering of the event. |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function init() |
|
52
|
|
|
{ |
|
53
|
2 |
|
parent::init(); |
|
54
|
2 |
|
$this->trigger(self::EVENT_INIT); |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Creates a DB command that can be used to execute this query. |
|
59
|
|
|
* @param Connection $db the DB connection used to create the DB command. |
|
60
|
|
|
* If null, the DB connection returned by [[modelClass]] will be used. |
|
61
|
|
|
* @return Command the created DB command instance |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function createCommand($db = null) |
|
64
|
|
|
{ |
|
65
|
2 |
|
if ($this->primaryModel !== null) { |
|
66
|
|
|
// lazy loading |
|
67
|
|
|
if (is_array($this->via)) { |
|
68
|
|
|
// via relation |
|
69
|
|
|
/** @var $viaQuery ActiveQuery */ |
|
70
|
|
|
list($viaName, $viaQuery) = $this->via; |
|
71
|
|
|
if ($viaQuery->multiple) { |
|
72
|
|
|
$viaModels = $viaQuery->all(); |
|
73
|
|
|
$this->primaryModel->populateRelation($viaName, $viaModels); |
|
74
|
|
|
} else { |
|
75
|
|
|
$model = $viaQuery->one(); |
|
76
|
|
|
$this->primaryModel->populateRelation($viaName, $model); |
|
77
|
|
|
$viaModels = $model === null ? [] : [$model]; |
|
78
|
|
|
} |
|
79
|
|
|
$this->filterByModels($viaModels); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->filterByModels([$this->primaryModel]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/* @var $modelClass ActiveRecord */ |
|
86
|
2 |
|
$modelClass = $this->modelClass; |
|
87
|
|
|
|
|
88
|
2 |
|
if ($db === null) { |
|
89
|
2 |
|
$db = $modelClass::getDb(); |
|
90
|
2 |
|
} |
|
91
|
2 |
|
if ($this->from === null) { |
|
92
|
2 |
|
$this->from = $modelClass::from(); |
|
|
|
|
|
|
93
|
2 |
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$commandConfig = $db->getQueryBuilder()->build($this); |
|
96
|
|
|
|
|
97
|
2 |
|
return $db->createCommand($commandConfig); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Prepares query for use. See NOTE. |
|
102
|
|
|
* @param QueryBuilder $builder |
|
103
|
|
|
* @return static |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function prepare($builder = null) |
|
106
|
|
|
{ |
|
107
|
|
|
// NOTE: because the same ActiveQuery may be used to build different SQL statements |
|
108
|
|
|
// (e.g. by ActiveDataProvider, one for count query, the other for row data query, |
|
109
|
|
|
// it is important to make sure the same ActiveQuery can be used to build SQL statements |
|
110
|
|
|
// multiple times. |
|
111
|
2 |
|
if (!empty($this->joinWith)) { |
|
112
|
|
|
$this->buildJoinWith(); |
|
113
|
|
|
$this->joinWith = null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $with |
|
121
|
|
|
* @return static |
|
122
|
|
|
*/ |
|
123
|
|
|
public function joinWith($with) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->joinWith[] = (array) $with; |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function buildJoinWith() |
|
131
|
|
|
{ |
|
132
|
|
|
$join = $this->join; |
|
133
|
|
|
$this->join = []; |
|
134
|
|
|
|
|
135
|
|
|
$model = new $this->modelClass(); |
|
136
|
|
|
|
|
137
|
|
|
foreach ($this->joinWith as $with) { |
|
|
|
|
|
|
138
|
|
|
$this->joinWithRelations($model, $with); |
|
139
|
|
|
|
|
140
|
|
|
foreach ($with as $name => $callback) { |
|
141
|
|
|
if (is_int($name)) { |
|
142
|
|
|
$this->innerJoin([$callback]); |
|
143
|
|
|
} else { |
|
144
|
|
|
$this->innerJoin([$name => $callback]); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
unset($with[$name]); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (!empty($join)) { |
|
152
|
|
|
// append explicit join to joinWith() |
|
153
|
|
|
// https://github.com/yiisoft/yii2/issues/2880 |
|
154
|
|
|
$this->join = empty($this->join) ? $join : array_merge($this->join, $join); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (empty($this->select) || true) { |
|
158
|
|
|
$this->addSelect(['*' => '*']); |
|
159
|
|
|
foreach ($this->joinWith as $join) { |
|
|
|
|
|
|
160
|
|
|
$key = array_shift(array_keys($join)); |
|
|
|
|
|
|
161
|
|
|
$closure = array_shift($join); |
|
162
|
|
|
|
|
163
|
|
|
$this->addSelect(is_int($key) ? $closure : $key); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param ActiveRecord $model |
|
170
|
|
|
* @param $with |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function joinWithRelations($model, $with) |
|
173
|
|
|
{ |
|
174
|
|
|
foreach ($with as $name => $callback) { |
|
175
|
|
|
if (is_int($name)) { |
|
176
|
|
|
$name = $callback; |
|
177
|
|
|
$callback = null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$primaryModel = $model; |
|
181
|
|
|
$parent = $this; |
|
182
|
|
|
|
|
183
|
|
|
if (!isset($relations[$name])) { |
|
184
|
|
|
$relations[$name] = $relation = $primaryModel->getRelation($name); |
|
|
|
|
|
|
185
|
|
|
if ($callback !== null) { |
|
186
|
|
|
call_user_func($callback, $relation); |
|
187
|
|
|
} |
|
188
|
|
|
if (!empty($relation->joinWith)) { |
|
|
|
|
|
|
189
|
|
|
$relation->buildJoinWith(); |
|
190
|
|
|
} |
|
191
|
|
|
$this->joinWithRelation($parent, $relation); |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Joins a parent query with a child query. |
|
198
|
|
|
* The current query object will be modified accordingly. |
|
199
|
|
|
* @param ActiveQuery $parent |
|
200
|
|
|
* @param ActiveQuery $child |
|
201
|
|
|
*/ |
|
202
|
|
|
private function joinWithRelation($parent, $child) |
|
|
|
|
|
|
203
|
|
|
{ |
|
204
|
|
|
if (!empty($child->join)) { |
|
205
|
|
|
foreach ($child->join as $join) { |
|
206
|
|
|
$this->join[] = $join; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function select($columns, $option = null) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->select = $columns; |
|
214
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param array|string $columns |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
|
|
public function addSelect($columns) |
|
223
|
|
|
{ |
|
224
|
|
|
if (!is_array($columns)) { |
|
225
|
|
|
$columns = (array) $columns; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
if ($this->select === null) { |
|
229
|
|
|
$this->select = $columns; |
|
230
|
|
|
} else { |
|
231
|
|
|
$this->select = array_merge($this->select, $columns); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Executes query and returns a single row of result. |
|
239
|
|
|
* |
|
240
|
|
|
* @param Connection $db the DB connection used to create the DB command. |
|
241
|
|
|
* If null, the DB connection returned by [[modelClass]] will be used. |
|
242
|
|
|
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
|
243
|
|
|
* the query result may be either an array or an ActiveRecord object. Null will be returned |
|
244
|
|
|
* if the query results in nothing. |
|
245
|
|
|
*/ |
|
246
|
|
|
public function one($db = null) |
|
247
|
|
|
{ |
|
248
|
|
|
if ($this->asArray) { |
|
249
|
|
|
return parent::one($db); |
|
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$row = $this->searchOne($db); |
|
253
|
|
|
|
|
254
|
|
|
return reset($this->populate([$row])); |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Executes query and returns all results as an array. |
|
259
|
|
|
* @param Connection $db the DB connection used to create the DB command. |
|
260
|
|
|
* If null, the DB connection returned by [[modelClass]] will be used. |
|
261
|
|
|
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
|
262
|
|
|
*/ |
|
263
|
2 |
|
public function all($db = null) |
|
264
|
|
|
{ |
|
265
|
2 |
|
if ($this->asArray) { |
|
266
|
|
|
return parent::all($db); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
2 |
|
$rows = $this->searchAll(); |
|
270
|
|
|
|
|
271
|
|
|
return $this->populate($rows); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function populate($rows) |
|
275
|
|
|
{ |
|
276
|
|
|
if (empty($rows)) { |
|
277
|
|
|
return []; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$models = $this->createModels($rows); |
|
281
|
|
|
|
|
282
|
|
|
if (!empty($this->with)) { |
|
283
|
|
|
$this->findWith($this->with, $models); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
foreach ($models as $model) { |
|
287
|
|
|
$model->afterFind(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
return $models; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
private function createModels($rows) |
|
294
|
|
|
{ |
|
295
|
|
|
$models = []; |
|
296
|
|
|
$class = $this->modelClass; |
|
297
|
|
|
foreach ($rows as $row) { |
|
298
|
|
|
$model = $class::instantiate($row); |
|
299
|
|
|
$modelClass = get_class($model); |
|
300
|
|
|
$modelClass::populateRecord($model, $row); |
|
301
|
|
|
$this->populateJoinedRelations($model, $row); |
|
302
|
|
|
if ($this->indexBy) { |
|
303
|
|
View Code Duplication |
if ($this->indexBy instanceof \Closure) { |
|
|
|
|
|
|
304
|
|
|
$key = call_user_func($this->indexBy, $model); |
|
305
|
|
|
} else { |
|
306
|
|
|
$key = $model->{$this->indexBy}; |
|
307
|
|
|
} |
|
308
|
|
|
$models[$key] = $model; |
|
309
|
|
|
} else { |
|
310
|
|
|
$models[] = $model; |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return $models; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Populates joined relations from [[join]] array. |
|
319
|
|
|
* |
|
320
|
|
|
* @param ActiveRecord $model |
|
321
|
|
|
* @param array $row |
|
322
|
|
|
*/ |
|
323
|
|
|
public function populateJoinedRelations($model, array $row) |
|
324
|
|
|
{ |
|
325
|
|
|
foreach ($row as $key => $value) { |
|
326
|
|
|
if (empty($this->join) || !is_array($value) || $model->hasAttribute($key)) { |
|
327
|
|
|
continue; |
|
328
|
|
|
} |
|
329
|
|
|
foreach ($this->join as $join) { |
|
330
|
|
|
$name = array_shift(array_keys($join)); |
|
|
|
|
|
|
331
|
|
|
$closure = array_shift($join); |
|
332
|
|
|
|
|
333
|
|
|
if (is_int($name)) { |
|
334
|
|
|
$name = $closure; |
|
335
|
|
|
$closure = null; |
|
336
|
|
|
} |
|
337
|
|
|
if ($name !== $key) { |
|
338
|
|
|
continue; |
|
339
|
|
|
} |
|
340
|
|
|
if ($model->isRelationPopulated($name)) { |
|
341
|
|
|
continue 2; |
|
342
|
|
|
} |
|
343
|
|
|
$records = []; |
|
344
|
|
|
$relation = $model->getRelation($name); |
|
345
|
|
|
$relationClass = $relation->modelClass; |
|
|
|
|
|
|
346
|
|
|
if ($closure !== null) { |
|
347
|
|
|
call_user_func($closure, $relation); |
|
348
|
|
|
} |
|
349
|
|
|
$relation->prepare(); |
|
350
|
|
|
|
|
351
|
|
|
if ($relation->multiple) { |
|
|
|
|
|
|
352
|
|
|
foreach ($value as $item) { |
|
353
|
|
|
$relatedModel = $relationClass::instantiate($item); |
|
354
|
|
|
$relatedModelClass = get_class($relatedModel); |
|
355
|
|
|
$relatedModelClass::populateRecord($relatedModel, $item); |
|
356
|
|
|
$relation->populateJoinedRelations($relatedModel, $item); |
|
357
|
|
|
$relation->addInverseRelation($relatedModel, $model); |
|
358
|
|
|
if ($relation->indexBy !== null) { |
|
|
|
|
|
|
359
|
|
|
$index = is_string($relation->indexBy) |
|
|
|
|
|
|
360
|
|
|
? $relatedModel[$relation->indexBy] |
|
|
|
|
|
|
361
|
|
|
: call_user_func($relation->indexBy, $relatedModel); |
|
|
|
|
|
|
362
|
|
|
$records[$index] = $relatedModel; |
|
363
|
|
|
} else { |
|
364
|
|
|
$records[] = $relatedModel; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
} else { |
|
368
|
|
|
$relatedModel = $relationClass::instantiate($value); |
|
369
|
|
|
$relatedModelClass = get_class($relatedModel); |
|
370
|
|
|
$relatedModelClass::populateRecord($relatedModel, $value); |
|
371
|
|
|
$relation->populateJoinedRelations($relatedModel, $value); |
|
372
|
|
|
$relation->addInverseRelation($relatedModel, $model); |
|
373
|
|
|
$records = $relatedModel; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
$model->populateRelation($name, $records); |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* @param $relatedModel |
|
383
|
|
|
*/ |
|
384
|
|
|
private function addInverseRelation($relatedModel) |
|
385
|
|
|
{ |
|
386
|
|
|
if ($this->inverseOf === null) { |
|
387
|
|
|
return; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
$inverseRelation = $relatedModel->getRelation($this->inverseOf); |
|
391
|
|
|
$relatedModel->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$this->primaryModel] : $this->primaryModel); |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..