|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\ActiveRecord; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use ReflectionException; |
|
9
|
|
|
use Throwable; |
|
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
11
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
12
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
14
|
|
|
|
|
15
|
|
|
use function array_column; |
|
16
|
|
|
use function array_combine; |
|
17
|
|
|
use function array_diff_key; |
|
18
|
|
|
use function array_fill_keys; |
|
19
|
|
|
use function array_filter; |
|
20
|
|
|
use function array_flip; |
|
21
|
|
|
use function array_intersect_key; |
|
22
|
|
|
use function array_keys; |
|
23
|
|
|
use function array_merge; |
|
24
|
|
|
use function array_unique; |
|
25
|
|
|
use function count; |
|
26
|
|
|
use function is_array; |
|
27
|
|
|
use function is_object; |
|
28
|
|
|
use function is_string; |
|
29
|
|
|
use function key; |
|
30
|
|
|
use function reset; |
|
31
|
|
|
use function serialize; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ActiveRelationTrait implements the common methods and properties for active record relational queries. |
|
35
|
|
|
*/ |
|
36
|
|
|
trait ActiveRelationTrait |
|
37
|
|
|
{ |
|
38
|
|
|
private bool $multiple = false; |
|
39
|
|
|
private ActiveRecordInterface|null $primaryModel = null; |
|
40
|
|
|
/** @psalm-var string[] */ |
|
41
|
|
|
private array $link = []; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string|null the name of the relation that is the inverse of this relation. |
|
44
|
|
|
* |
|
45
|
|
|
* For example, an order has a customer, which means the inverse of the "customer" relation is the "orders", and the |
|
46
|
|
|
* inverse of the "orders" relation is the "customer". If this property is set, the primary record(s) will be |
|
47
|
|
|
* referenced through the specified relation. |
|
48
|
|
|
* |
|
49
|
|
|
* For example, `$customer->orders[0]->customer` and `$customer` will be the same object, and accessing the customer |
|
50
|
|
|
* of an order will not trigger new DB query. |
|
51
|
|
|
* |
|
52
|
|
|
* This property is only used in relational context. |
|
53
|
|
|
* |
|
54
|
|
|
* {@see inverseOf()} |
|
55
|
|
|
*/ |
|
56
|
|
|
private string|null $inverseOf = null; |
|
57
|
|
|
private array|ActiveQuery|null $via = null; |
|
58
|
|
|
private array $viaMap = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Clones internal objects. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __clone() |
|
64
|
|
|
{ |
|
65
|
|
|
/** make a clone of "via" object so that the same query object can be reused multiple times */ |
|
66
|
|
|
if (is_object($this->via)) { |
|
67
|
|
|
$this->via = clone $this->via; |
|
68
|
|
|
} elseif (is_array($this->via)) { |
|
69
|
|
|
$this->via = [$this->via[0], clone $this->via[1], $this->via[2]]; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Specifies the relation associated with the junction table. |
|
75
|
|
|
* |
|
76
|
|
|
* Use this method to specify a pivot record/table when declaring a relation in the {@see ActiveRecord} class: |
|
77
|
|
|
* |
|
78
|
|
|
* ```php |
|
79
|
|
|
* class Order extends ActiveRecord |
|
80
|
|
|
* { |
|
81
|
|
|
* public function getOrderItems() { |
|
82
|
|
|
* return $this->hasMany(OrderItem::class, ['order_id' => 'id']); |
|
83
|
|
|
* } |
|
84
|
|
|
* |
|
85
|
|
|
* public function getItems() { |
|
86
|
|
|
* return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems'); |
|
87
|
|
|
* } |
|
88
|
|
|
* } |
|
89
|
|
|
* ``` |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $relationName the relation name. This refers to a relation declared in {@see primaryModel}. |
|
92
|
|
|
* @param callable|null $callable a PHP callback for customizing the relation associated with the junction table. |
|
93
|
|
|
* Its signature should be `function($query)`, where `$query` is the query to be customized. |
|
94
|
|
|
* |
|
95
|
|
|
* @return static the relation object itself. |
|
96
|
|
|
*/ |
|
97
|
|
|
public function via(string $relationName, callable $callable = null): static |
|
98
|
|
|
{ |
|
99
|
|
|
$relation = $this->primaryModel?->relationQuery($relationName); |
|
100
|
|
|
$callableUsed = $callable !== null; |
|
101
|
|
|
$this->via = [$relationName, $relation, $callableUsed]; |
|
102
|
107 |
|
|
|
103
|
|
|
if ($callableUsed) { |
|
104
|
107 |
|
$callable($relation); |
|
105
|
107 |
|
} |
|
106
|
107 |
|
|
|
107
|
|
|
return $this; |
|
108
|
107 |
|
} |
|
109
|
73 |
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Sets the name of the relation that is the inverse of this relation. |
|
112
|
107 |
|
* |
|
113
|
|
|
* For example, a customer has orders, which means the inverse of the "orders" relation is the "customer". |
|
114
|
|
|
* |
|
115
|
|
|
* If this property is set, the primary record(s) will be referenced through the specified relation. |
|
116
|
|
|
* |
|
117
|
|
|
* For example, `$customer->orders[0]->customer` and `$customer` will be the same object, and accessing the customer |
|
118
|
|
|
* of an order will not trigger a new DB query. |
|
119
|
|
|
* |
|
120
|
|
|
* Use this method when declaring a relation in the {@see ActiveRecord} class, e.g. in Customer model: |
|
121
|
|
|
* |
|
122
|
|
|
* ```php |
|
123
|
|
|
* public function getOrders() |
|
124
|
|
|
* { |
|
125
|
|
|
* return $this->hasMany(Order::class, ['customer_id' => 'id'])->inverseOf('customer'); |
|
126
|
|
|
* } |
|
127
|
|
|
* ``` |
|
128
|
|
|
* |
|
129
|
|
|
* This also may be used for Order model, but with caution: |
|
130
|
|
|
* |
|
131
|
|
|
* ```php |
|
132
|
|
|
* public function getCustomer() |
|
133
|
|
|
* { |
|
134
|
|
|
* return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders'); |
|
135
|
|
|
* } |
|
136
|
|
|
* ``` |
|
137
|
|
|
* |
|
138
|
|
|
* in this case result will depend on how order(s) was loaded. |
|
139
|
|
|
* Let's suppose customer has several orders. If only one order was loaded: |
|
140
|
|
|
* |
|
141
|
|
|
* ```php |
|
142
|
|
|
* $orderQuery = new ActiveQuery(Order::class, $db); |
|
143
|
|
|
* $orders = $orderQuery->where(['id' => 1])->all(); |
|
144
|
|
|
* $customerOrders = $orders[0]->customer->orders; |
|
145
|
|
|
* ``` |
|
146
|
|
|
* |
|
147
|
|
|
* variable `$customerOrders` will contain only one order. If orders was loaded like this: |
|
148
|
|
|
* |
|
149
|
|
|
* ```php |
|
150
|
|
|
* $orderQuery = new ActiveQuery(Order::class, $db); |
|
151
|
|
|
* $orders = $orderQuery->with('customer')->where(['customer_id' => 1])->all(); |
|
152
|
|
|
* $customerOrders = $orders[0]->customer->orders; |
|
153
|
|
|
* ``` |
|
154
|
|
|
* |
|
155
|
|
|
* variable `$customerOrders` will contain all orders of the customer. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $relationName the name of the relation that is the inverse of this relation. |
|
158
|
|
|
* |
|
159
|
|
|
* @return static the relation object itself. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function inverseOf(string $relationName): static |
|
162
|
|
|
{ |
|
163
|
|
|
$this->inverseOf = $relationName; |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
16 |
|
} |
|
167
|
|
|
|
|
168
|
16 |
|
/** |
|
169
|
|
|
* Returns query records depends on {@see $multiple} . |
|
170
|
16 |
|
* |
|
171
|
|
|
* This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion. |
|
172
|
|
|
* |
|
173
|
|
|
* @throws Exception |
|
174
|
|
|
* @throws InvalidArgumentException |
|
175
|
|
|
* @throws InvalidConfigException |
|
176
|
|
|
* @throws ReflectionException |
|
177
|
|
|
* @throws Throwable if the relation is invalid. |
|
178
|
|
|
* |
|
179
|
|
|
* @return ActiveRecordInterface|array|null the related record(s). |
|
180
|
|
|
*/ |
|
181
|
|
|
public function relatedRecords(): ActiveRecordInterface|array|null |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->multiple ? $this->all() : $this->onePopulate(); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
101 |
|
/** |
|
187
|
|
|
* If applicable, populate the query's primary model into the related records' inverse relationship. |
|
188
|
101 |
|
* |
|
189
|
101 |
|
* @param array $result the array of related records as generated by {@see populate()} |
|
190
|
101 |
|
* |
|
191
|
101 |
|
* @throws \Yiisoft\Definitions\Exception\InvalidConfigException |
|
192
|
|
|
*/ |
|
193
|
|
|
private function addInverseRelations(array &$result): void |
|
194
|
|
|
{ |
|
195
|
|
|
if ($this->inverseOf === null) { |
|
196
|
|
|
return; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
101 |
|
$relatedModel = reset($result); |
|
200
|
|
|
|
|
201
|
|
|
if ($relatedModel instanceof ActiveRecordInterface) { |
|
202
|
|
|
$inverseRelation = $relatedModel->relationQuery($this->inverseOf); |
|
203
|
|
|
$primaryModel = $inverseRelation->getMultiple() ? [$this->primaryModel] : $this->primaryModel; |
|
204
|
|
|
|
|
205
|
|
|
foreach ($result as $relatedModel) { |
|
206
|
|
|
$relatedModel->populateRelation($this->inverseOf, $primaryModel); |
|
207
|
16 |
|
} |
|
208
|
|
|
} else { |
|
209
|
16 |
|
$inverseRelation = $this->getARInstance()->relationQuery($this->inverseOf); |
|
|
|
|
|
|
210
|
|
|
$primaryModel = $inverseRelation->getMultiple() ? [$this->primaryModel] : $this->primaryModel; |
|
211
|
|
|
|
|
212
|
|
|
foreach ($result as &$relatedModel) { |
|
213
|
16 |
|
$relatedModel[$this->inverseOf] = $primaryModel; |
|
214
|
16 |
|
} |
|
215
|
16 |
|
} |
|
216
|
16 |
|
} |
|
217
|
|
|
|
|
218
|
16 |
|
/** |
|
219
|
16 |
|
* Finds the related records and populates them into the primary models. |
|
220
|
16 |
|
* |
|
221
|
|
|
* @param string $name the relation name |
|
222
|
|
|
* @param array $primaryModels primary models |
|
223
|
8 |
|
* |
|
224
|
8 |
|
* @throws InvalidArgumentException|InvalidConfigException|NotSupportedException|Throwable if {@see link()} is |
|
225
|
|
|
* invalid. |
|
226
|
|
|
* @throws Exception |
|
227
|
8 |
|
* |
|
228
|
8 |
|
* @return array the related models |
|
229
|
|
|
*/ |
|
230
|
|
|
public function populateRelation(string $name, array &$primaryModels): array |
|
231
|
16 |
|
{ |
|
232
|
|
|
if ($this->via instanceof self) { |
|
233
|
|
|
$viaQuery = $this->via; |
|
234
|
|
|
$viaModels = $viaQuery->findJunctionRows($primaryModels); |
|
235
|
|
|
$this->filterByModels($viaModels); |
|
236
|
|
|
} elseif (is_array($this->via)) { |
|
237
|
|
|
[$viaName, $viaQuery] = $this->via; |
|
238
|
|
|
|
|
239
|
|
|
if ($viaQuery->asArray === null) { |
|
240
|
|
|
/** inherit asArray from primary query */ |
|
241
|
|
|
$viaQuery->asArray($this->asArray); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$viaQuery->primaryModel = null; |
|
245
|
139 |
|
$viaModels = $viaQuery->populateRelation($viaName, $primaryModels); |
|
246
|
|
|
$this->filterByModels($viaModels); |
|
247
|
139 |
|
} else { |
|
248
|
|
|
$this->filterByModels($primaryModels); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
139 |
|
if (!$this->multiple && count($primaryModels) === 1) { |
|
252
|
|
|
$models = [$this->onePopulate()]; |
|
253
|
|
|
$this->populateInverseRelation($models, $primaryModels); |
|
254
|
|
|
|
|
255
|
|
|
$primaryModel = reset($primaryModels); |
|
256
|
|
|
|
|
257
|
12 |
|
if ($primaryModel instanceof ActiveRecordInterface) { |
|
258
|
12 |
|
$primaryModel->populateRelation($name, $models[0]); |
|
259
|
12 |
|
} else { |
|
260
|
139 |
|
$primaryModels[key($primaryModels)][$name] = $models[0]; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $models; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
72 |
|
/** |
|
267
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/3197} |
|
268
|
72 |
|
* |
|
269
|
|
|
* delay indexing related models after buckets are built. |
|
270
|
72 |
|
*/ |
|
271
|
|
|
$indexBy = $this->getIndexBy(); |
|
|
|
|
|
|
272
|
|
|
$this->indexBy(null); |
|
|
|
|
|
|
273
|
72 |
|
$models = $this->all(); |
|
274
|
72 |
|
|
|
275
|
72 |
|
$this->populateInverseRelation($models, $primaryModels); |
|
276
|
|
|
|
|
277
|
139 |
|
if (isset($viaModels, $viaQuery)) { |
|
278
|
|
|
$buckets = $this->buildBuckets($models, $viaModels, $viaQuery); |
|
279
|
|
|
} else { |
|
280
|
139 |
|
$buckets = $this->buildBuckets($models); |
|
281
|
28 |
|
} |
|
282
|
28 |
|
|
|
283
|
|
|
$this->indexBy($indexBy); |
|
284
|
28 |
|
|
|
285
|
28 |
|
if ($indexBy !== null && $this->multiple) { |
|
286
|
|
|
$buckets = $this->indexBuckets($buckets, $indexBy); |
|
287
|
4 |
|
} |
|
288
|
|
|
|
|
289
|
|
|
if (isset($viaQuery)) { |
|
290
|
28 |
|
$deepViaQuery = $viaQuery; |
|
291
|
8 |
|
|
|
292
|
|
|
while ($deepViaQuery->via) { |
|
293
|
|
|
$deepViaQuery = is_array($deepViaQuery->via) ? $deepViaQuery->via[1] : $deepViaQuery->via; |
|
294
|
28 |
|
} |
|
295
|
|
|
|
|
296
|
|
|
$link = $deepViaQuery->link; |
|
297
|
|
|
} else { |
|
298
|
|
|
$link = $this->link; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$this->populateRelationFromBuckets($primaryModels, $buckets, $name, $link); |
|
302
|
123 |
|
|
|
303
|
123 |
|
return $models; |
|
304
|
123 |
|
} |
|
305
|
|
|
|
|
306
|
123 |
|
/** |
|
307
|
72 |
|
* @throws \Yiisoft\Definitions\Exception\InvalidConfigException |
|
308
|
|
|
*/ |
|
309
|
123 |
|
private function populateInverseRelation( |
|
310
|
|
|
array &$models, |
|
311
|
|
|
array $primaryModels, |
|
312
|
123 |
|
): void { |
|
313
|
|
|
if ($this->inverseOf === null || empty($models) || empty($primaryModels)) { |
|
314
|
123 |
|
return; |
|
315
|
17 |
|
} |
|
316
|
|
|
|
|
317
|
|
|
$name = $this->inverseOf; |
|
318
|
123 |
|
$model = reset($models); |
|
319
|
123 |
|
|
|
320
|
72 |
|
/** @var ActiveQuery $relation */ |
|
321
|
|
|
$relation = is_array($model) |
|
322
|
72 |
|
? $this->getARInstance()->relationQuery($name) |
|
323
|
5 |
|
: $model->relationQuery($name); |
|
324
|
|
|
|
|
325
|
|
|
$link = $relation->getLink(); |
|
326
|
72 |
|
$indexBy = $relation->getIndexBy(); |
|
327
|
|
|
$buckets = $relation->buildBuckets($primaryModels); |
|
328
|
|
|
|
|
329
|
123 |
|
if ($indexBy !== null && $relation->getMultiple()) { |
|
330
|
123 |
|
$buckets = $this->indexBuckets($buckets, $indexBy); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
$relation->populateRelationFromBuckets($models, $buckets, $name, $link); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
private function populateRelationFromBuckets( |
|
337
|
|
|
array &$models, |
|
338
|
|
|
array $buckets, |
|
339
|
|
|
string $name, |
|
340
|
|
|
array $link |
|
341
|
|
|
): void { |
|
342
|
|
|
$indexBy = $this->getIndexBy(); |
|
343
|
|
|
$default = $this->multiple ? [] : null; |
|
344
|
|
|
|
|
345
|
|
|
foreach ($models as &$model) { |
|
346
|
123 |
|
$keys = $this->getModelKeys($model, $link); |
|
347
|
123 |
|
|
|
348
|
|
|
/** @psalm-suppress NamedArgumentNotAllowed */ |
|
349
|
|
|
$value = match (count($keys)) { |
|
350
|
123 |
|
0 => $default, |
|
351
|
123 |
|
1 => $buckets[$keys[0]] ?? $default, |
|
352
|
|
|
default => !$this->multiple |
|
353
|
9 |
|
? $default |
|
354
|
|
|
: ($indexBy !== null |
|
355
|
|
|
? array_replace(...array_intersect_key($buckets, array_flip($keys))) |
|
356
|
123 |
|
: array_merge(...array_intersect_key($buckets, array_flip($keys)))), |
|
357
|
8 |
|
}; |
|
358
|
|
|
|
|
359
|
|
|
if ($model instanceof ActiveRecordInterface) { |
|
360
|
123 |
|
$model->populateRelation($name, $value); |
|
361
|
|
|
} else { |
|
362
|
|
|
$model[$name] = $value; |
|
363
|
12 |
|
} |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
private function buildBuckets( |
|
368
|
|
|
array $models, |
|
369
|
12 |
|
array $viaModels = null, |
|
370
|
|
|
self $viaQuery = null |
|
371
|
|
|
): array { |
|
372
|
12 |
|
if ($viaModels !== null) { |
|
373
|
|
|
$map = []; |
|
374
|
|
|
$linkValues = $this->link; |
|
375
|
12 |
|
$viaLink = $viaQuery->link ?? []; |
|
376
|
12 |
|
$viaLinkKeys = array_keys($viaLink); |
|
377
|
|
|
$viaVia = null; |
|
378
|
4 |
|
|
|
379
|
|
|
foreach ($viaModels as $viaModel) { |
|
380
|
|
|
$key1 = $this->getModelKeys($viaModel, $viaLinkKeys); |
|
381
|
12 |
|
$key2 = $this->getModelKeys($viaModel, $linkValues); |
|
382
|
8 |
|
$flags = array_fill_keys($key1, true); |
|
383
|
8 |
|
|
|
384
|
8 |
|
foreach ($key2 as $key) { |
|
385
|
8 |
|
if (isset($map[$key])) { |
|
386
|
8 |
|
$map[$key] += $flags; |
|
387
|
|
|
} else { |
|
388
|
|
|
$map[$key] = $flags; |
|
389
|
8 |
|
} |
|
390
|
4 |
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
if ($viaQuery !== null) { |
|
394
|
|
|
$viaQuery->viaMap = $map; |
|
395
|
4 |
|
$viaVia = $viaQuery->getVia(); |
|
396
|
4 |
|
} |
|
397
|
4 |
|
|
|
398
|
|
|
while ($viaVia) { |
|
399
|
|
|
/** |
|
400
|
|
|
* @var ActiveQuery $viaViaQuery |
|
401
|
8 |
|
* |
|
402
|
8 |
|
* @psalm-suppress RedundantCondition |
|
403
|
8 |
|
*/ |
|
404
|
8 |
|
$viaViaQuery = is_array($viaVia) ? $viaVia[1] : $viaVia; |
|
405
|
8 |
|
$map = $this->mapVia($map, $viaViaQuery->viaMap); |
|
|
|
|
|
|
406
|
|
|
|
|
407
|
4 |
|
$viaVia = $viaViaQuery->getVia(); |
|
408
|
|
|
} |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
$buckets = []; |
|
412
|
|
|
$linkKeys = array_keys($this->link); |
|
413
|
|
|
|
|
414
|
|
|
if (isset($map)) { |
|
415
|
|
|
foreach ($models as $model) { |
|
416
|
|
|
$keys = $this->getModelKeys($model, $linkKeys); |
|
417
|
|
|
/** @var bool[][] $filtered */ |
|
418
|
|
|
$filtered = array_intersect_key($map, array_flip($keys)); |
|
419
|
|
|
|
|
420
|
12 |
|
foreach ($filtered as $keys2) { |
|
421
|
|
|
foreach (array_keys($keys2) as $key2) { |
|
422
|
127 |
|
$buckets[$key2][] = $model; |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
} |
|
426
|
|
|
} else { |
|
427
|
|
|
foreach ($models as $model) { |
|
428
|
|
|
$keys = $this->getModelKeys($model, $linkKeys); |
|
429
|
127 |
|
|
|
430
|
72 |
|
foreach ($keys as $key) { |
|
431
|
72 |
|
$buckets[$key][] = $model; |
|
432
|
72 |
|
} |
|
433
|
72 |
|
} |
|
434
|
|
|
} |
|
435
|
72 |
|
|
|
436
|
71 |
|
if (!$this->multiple) { |
|
437
|
71 |
|
return array_combine( |
|
438
|
71 |
|
array_keys($buckets), |
|
439
|
|
|
array_column($buckets, 0) |
|
440
|
|
|
); |
|
441
|
72 |
|
} |
|
442
|
|
|
|
|
443
|
72 |
|
return $buckets; |
|
444
|
72 |
|
} |
|
445
|
5 |
|
|
|
446
|
5 |
|
private function mapVia(array $map, array $viaMap): array |
|
447
|
|
|
{ |
|
448
|
5 |
|
$resultMap = []; |
|
449
|
|
|
|
|
450
|
|
|
foreach ($map as $key => $linkKeys) { |
|
451
|
|
|
$resultMap[$key] = []; |
|
452
|
127 |
|
foreach (array_keys($linkKeys) as $linkKey) { |
|
453
|
127 |
|
/** @psalm-suppress InvalidArrayOffset */ |
|
454
|
|
|
$resultMap[$key] += $viaMap[$linkKey]; |
|
455
|
127 |
|
} |
|
456
|
72 |
|
} |
|
457
|
71 |
|
|
|
458
|
71 |
|
return $resultMap; |
|
459
|
71 |
|
} |
|
460
|
71 |
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* Indexes buckets by a column name. |
|
463
|
|
|
* |
|
464
|
|
|
* @param Closure|string $indexBy the name of the column by which the query results should be indexed by. This can |
|
465
|
127 |
|
* also be a {@see Closure} that returns the index value based on the given models data. |
|
466
|
127 |
|
*/ |
|
467
|
127 |
|
private function indexBuckets(array $buckets, Closure|string $indexBy): array |
|
468
|
|
|
{ |
|
469
|
|
|
foreach ($buckets as &$models) { |
|
470
|
|
|
$models = ArArrayHelper::index($models, $indexBy); |
|
471
|
127 |
|
} |
|
472
|
56 |
|
|
|
473
|
56 |
|
return $buckets; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
127 |
|
* @param array $attributes the attributes to prefix. |
|
478
|
|
|
* |
|
479
|
|
|
* @throws \Yiisoft\Definitions\Exception\InvalidConfigException |
|
480
|
5 |
|
*/ |
|
481
|
|
|
private function prefixKeyColumns(array $attributes): array |
|
482
|
5 |
|
{ |
|
483
|
|
|
if (!empty($this->join) || !empty($this->joinWith)) { |
|
484
|
5 |
|
if (empty($this->from)) { |
|
485
|
5 |
|
$alias = $this->getARInstance()->getTableName(); |
|
486
|
5 |
|
} else { |
|
487
|
|
|
foreach ($this->from as $alias => $table) { |
|
488
|
|
|
if (!is_string($alias)) { |
|
489
|
|
|
$alias = $table; |
|
490
|
5 |
|
} |
|
491
|
|
|
break; |
|
492
|
|
|
} |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
if (isset($alias)) { |
|
496
|
|
|
foreach ($attributes as $i => $attribute) { |
|
497
|
|
|
$attributes[$i] = "$alias.$attribute"; |
|
498
|
|
|
} |
|
499
|
|
|
} |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
17 |
|
return $attributes; |
|
503
|
|
|
} |
|
504
|
17 |
|
|
|
505
|
|
|
/** |
|
506
|
17 |
|
* @throws \Yiisoft\Definitions\Exception\InvalidConfigException |
|
507
|
17 |
|
*/ |
|
508
|
17 |
|
protected function filterByModels(array $models): void |
|
509
|
17 |
|
{ |
|
510
|
17 |
|
$attributes = array_keys($this->link); |
|
511
|
|
|
$attributes = $this->prefixKeyColumns($attributes); |
|
512
|
|
|
|
|
513
|
|
|
$model = reset($models); |
|
514
|
17 |
|
$values = []; |
|
515
|
|
|
|
|
516
|
|
|
if (count($attributes) === 1) { |
|
517
|
|
|
/** single key */ |
|
518
|
|
|
$attribute = reset($this->link); |
|
519
|
|
|
|
|
520
|
|
|
if ($model instanceof ActiveRecordInterface) { |
|
521
|
|
|
foreach ($models as $model) { |
|
522
|
236 |
|
$value = $model->getAttribute($attribute); |
|
523
|
|
|
|
|
524
|
236 |
|
if ($value !== null) { |
|
525
|
36 |
|
if (is_array($value)) { |
|
526
|
12 |
|
$values = [...$values, ...$value]; |
|
527
|
|
|
} else { |
|
528
|
32 |
|
$values[] = $value; |
|
529
|
32 |
|
} |
|
530
|
32 |
|
} |
|
531
|
|
|
} |
|
532
|
32 |
|
} else { |
|
533
|
|
|
foreach ($models as $model) { |
|
534
|
|
|
if (isset($model[$attribute])) { |
|
535
|
|
|
$value = $model[$attribute]; |
|
536
|
36 |
|
|
|
537
|
36 |
|
if (is_array($value)) { |
|
538
|
36 |
|
$values = [...$values, ...$value]; |
|
539
|
|
|
} else { |
|
540
|
|
|
$values[] = $value; |
|
541
|
|
|
} |
|
542
|
|
|
} |
|
543
|
236 |
|
} |
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
236 |
|
if (!empty($values)) { |
|
547
|
|
|
$scalarValues = array_filter($values, 'is_scalar'); |
|
548
|
236 |
|
$nonScalarValues = array_diff_key($values, $scalarValues); |
|
549
|
|
|
|
|
550
|
236 |
|
$scalarValues = array_unique($scalarValues); |
|
551
|
|
|
$values = [...$scalarValues, ...$nonScalarValues]; |
|
552
|
236 |
|
} |
|
553
|
236 |
|
} else { |
|
554
|
|
|
$nulls = array_fill_keys($this->link, null); |
|
555
|
232 |
|
|
|
556
|
232 |
|
if ($model instanceof ActiveRecordInterface) { |
|
557
|
232 |
|
foreach ($models as $model) { |
|
558
|
228 |
|
$value = $model->getAttributes($this->link); |
|
559
|
|
|
|
|
560
|
228 |
|
if (!empty($value)) { |
|
561
|
|
|
$values[] = array_combine($attributes, array_merge($nulls, $value)); |
|
562
|
|
|
} |
|
563
|
228 |
|
} |
|
564
|
|
|
} else { |
|
565
|
|
|
foreach ($models as $model) { |
|
566
|
|
|
$value = array_intersect_key($model, $nulls); |
|
567
|
|
|
|
|
568
|
232 |
|
if (!empty($value)) { |
|
569
|
232 |
|
$values[] = array_combine($attributes, array_merge($nulls, $value)); |
|
570
|
|
|
} |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
8 |
|
if (empty($values)) { |
|
576
|
|
|
$this->emulateExecution(); |
|
|
|
|
|
|
577
|
8 |
|
$this->andWhere('1=0'); |
|
|
|
|
|
|
578
|
8 |
|
return; |
|
579
|
|
|
} |
|
580
|
8 |
|
|
|
581
|
8 |
|
$this->andWhere(['in', $attributes, $values]); |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
8 |
|
private function getModelKeys(ActiveRecordInterface|array $activeRecord, array $attributes): array |
|
585
|
|
|
{ |
|
586
|
8 |
|
$key = []; |
|
587
|
|
|
|
|
588
|
|
|
if (is_array($activeRecord)) { |
|
|
|
|
|
|
589
|
|
|
foreach ($attributes as $attribute) { |
|
590
|
|
|
if (isset($activeRecord[$attribute])) { |
|
591
|
|
|
$key[] = is_array($activeRecord[$attribute]) |
|
592
|
236 |
|
? $activeRecord[$attribute] |
|
593
|
232 |
|
: (string) $activeRecord[$attribute]; |
|
594
|
232 |
|
} |
|
595
|
232 |
|
} |
|
596
|
232 |
|
} else { |
|
597
|
228 |
|
foreach ($attributes as $attribute) { |
|
598
|
|
|
$value = $activeRecord->getAttribute($attribute); |
|
599
|
8 |
|
|
|
600
|
|
|
if ($value !== null) { |
|
601
|
|
|
$key[] = is_array($value) |
|
602
|
|
|
? $value |
|
603
|
232 |
|
: (string) $value; |
|
604
|
232 |
|
} |
|
605
|
|
|
} |
|
606
|
|
|
} |
|
607
|
236 |
|
|
|
608
|
236 |
|
return match (count($key)) { |
|
609
|
|
|
0 => [], |
|
610
|
|
|
1 => is_array($key[0]) ? $key[0] : [$key[0]], |
|
611
|
|
|
default => [serialize($key)], |
|
612
|
|
|
}; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
/** |
|
616
|
127 |
|
* @param array $primaryModels either array of AR instances or arrays. |
|
617
|
|
|
* |
|
618
|
127 |
|
* @throws Exception |
|
619
|
|
|
* @throws Throwable |
|
620
|
127 |
|
* @throws \Yiisoft\Definitions\Exception\InvalidConfigException |
|
621
|
127 |
|
*/ |
|
622
|
|
|
private function findJunctionRows(array $primaryModels): array |
|
623
|
|
|
{ |
|
624
|
127 |
|
if (empty($primaryModels)) { |
|
625
|
|
|
return []; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
127 |
|
$this->filterByModels($primaryModels); |
|
629
|
|
|
|
|
630
|
127 |
|
/* @var $primaryModel ActiveRecord */ |
|
631
|
|
|
$primaryModel = reset($primaryModels); |
|
632
|
|
|
|
|
633
|
|
|
if (!$primaryModel instanceof ActiveRecordInterface) { |
|
|
|
|
|
|
634
|
|
|
/** when primaryModels are array of arrays (asArray case) */ |
|
635
|
|
|
$primaryModel = $this->arClass; |
|
|
|
|
|
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
127 |
|
return $this->asArray()->all(); |
|
|
|
|
|
|
639
|
|
|
} |
|
640
|
127 |
|
|
|
641
|
|
|
public function getMultiple(): bool |
|
642
|
|
|
{ |
|
643
|
|
|
return $this->multiple; |
|
644
|
|
|
} |
|
645
|
|
|
|
|
646
|
|
|
/** |
|
647
|
|
|
* @return ActiveRecordInterface|null the primary model of a relational query. |
|
648
|
127 |
|
* |
|
649
|
|
|
* This is used only in lazy loading with dynamic query options. |
|
650
|
|
|
*/ |
|
651
|
|
|
public function getPrimaryModel(): ActiveRecordInterface|null |
|
652
|
|
|
{ |
|
653
|
|
|
return $this->primaryModel; |
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
28 |
|
/** |
|
657
|
|
|
* @psalm-return string[] |
|
658
|
28 |
|
*/ |
|
659
|
|
|
public function getLink(): array |
|
660
|
|
|
{ |
|
661
|
|
|
return $this->link; |
|
662
|
28 |
|
} |
|
663
|
|
|
|
|
664
|
|
|
public function getVia(): array|ActiveQueryInterface|null |
|
665
|
28 |
|
{ |
|
666
|
|
|
return $this->via; |
|
667
|
28 |
|
} |
|
668
|
|
|
|
|
669
|
|
|
public function multiple(bool $value): self |
|
670
|
|
|
{ |
|
671
|
|
|
$this->multiple = $value; |
|
672
|
28 |
|
|
|
673
|
|
|
return $this; |
|
674
|
|
|
} |
|
675
|
|
|
|
|
676
|
|
|
public function primaryModel(ActiveRecordInterface $value): self |
|
677
|
|
|
{ |
|
678
|
|
|
$this->primaryModel = $value; |
|
679
|
|
|
|
|
680
|
|
|
return $this; |
|
681
|
|
|
} |
|
682
|
39 |
|
|
|
683
|
|
|
public function link(array $value): self |
|
684
|
39 |
|
{ |
|
685
|
|
|
$this->link = $value; |
|
686
|
|
|
|
|
687
|
|
|
return $this; |
|
688
|
|
|
} |
|
689
|
|
|
} |
|
690
|
|
|
|