Passed
Pull Request — master (#361)
by Sergei
03:27
created

ActiveRelationTrait::populateRelationFromBuckets()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.3329

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 26
ccs 8
cts 12
cp 0.6667
rs 9.1111
cc 6
nc 6
nop 4
crap 7.3329
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();
0 ignored issues
show
Bug introduced by
It seems like all() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
        return $this->multiple ? $this->/** @scrutinizer ignore-call */ all() : $this->onePopulate();
Loading history...
Bug introduced by
It seems like onePopulate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
        return $this->multiple ? $this->all() : $this->/** @scrutinizer ignore-call */ onePopulate();
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like getARInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

209
            $inverseRelation = $this->/** @scrutinizer ignore-call */ getARInstance()->relationQuery($this->inverseOf);
Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like getIndexBy() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
        /** @scrutinizer ignore-call */ 
272
        $indexBy = $this->getIndexBy();
Loading history...
272
        $this->indexBy(null);
0 ignored issues
show
Bug introduced by
It seems like indexBy() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

272
        $this->/** @scrutinizer ignore-call */ 
273
               indexBy(null);
Loading history...
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
        $buckets = $relation->buildBuckets($primaryModels);
327
328
        if ($relation->getMultiple() && $relation->getIndexBy() !== null) {
329 123
            $buckets = $this->indexBuckets($buckets, $relation->getIndexBy());
330 123
        }
331
332
        $relation->populateRelationFromBuckets($models, $buckets, $name, $link);
333
    }
334
335
    private function populateRelationFromBuckets(
336
        array &$models,
337
        array $buckets,
338
        string $name,
339
        array $link
340
    ): void {
341
        $indexBy = $this->getIndexBy();
342
        $default = $this->multiple ? [] : null;
343
344
        foreach ($models as &$model) {
345
            $keys = $this->getModelKeys($model, $link);
346 123
347 123
            $value = match (count($keys)) {
348
                0 => $default,
349
                1 => $buckets[$keys[0]] ?? $default,
350 123
                default => !$this->multiple
351 123
                    ? $default
352
                    : ($indexBy !== null
353 9
                        ? array_replace(...array_intersect_key($buckets, array_flip($keys)))
354
                        : array_merge(...array_intersect_key($buckets, array_flip($keys)))),
355
            };
356 123
357 8
            if ($model instanceof ActiveRecordInterface) {
358
                $model->populateRelation($name, $value);
359
            } else {
360 123
                $model[$name] = $value;
361
            }
362
        }
363 12
    }
364
365
    private function buildBuckets(
366
        array $models,
367
        array $viaModels = null,
368
        self $viaQuery = null
369 12
    ): array {
370
        if ($viaModels !== null) {
371
            $map = [];
372 12
            $linkValues = $this->link;
373
            $viaLink = $viaQuery->link ?? [];
374
            $viaLinkKeys = array_keys($viaLink);
375 12
            $viaVia = null;
376 12
377
            foreach ($viaModels as $viaModel) {
378 4
                $key1 = $this->getModelKeys($viaModel, $viaLinkKeys);
379
                $key2 = $this->getModelKeys($viaModel, $linkValues);
380
                $flags = array_fill_keys($key1, true);
381 12
382 8
                foreach ($key2 as $key) {
383 8
                    if (isset($map[$key])) {
384 8
                        $map[$key] += $flags;
385 8
                    } else {
386 8
                        $map[$key] = $flags;
387
                    }
388
                }
389 8
            }
390 4
391
            if ($viaQuery !== null) {
392
                $viaQuery->viaMap = $map;
393
                $viaVia = $viaQuery->getVia();
394
            }
395 4
396 4
            while ($viaVia) {
397 4
                /**
398
                 * @var ActiveQuery $viaViaQuery
399
                 *
400
                 * @psalm-suppress RedundantCondition
401 8
                 */
402 8
                $viaViaQuery = is_array($viaVia) ? $viaVia[1] : $viaVia;
403 8
                $map = $this->mapVia($map, $viaViaQuery->viaMap);
0 ignored issues
show
Bug introduced by
The property viaMap is declared private in Yiisoft\ActiveRecord\ActiveQuery and cannot be accessed from this context.
Loading history...
404 8
405 8
                $viaVia = $viaViaQuery->getVia();
406
            }
407 4
        }
408
409
        $buckets = [];
410
        $linkKeys = array_keys($this->link);
411
412
        if (isset($map)) {
413
            foreach ($models as $model) {
414
                $keys = $this->getModelKeys($model, $linkKeys);
415
                $filtered = array_intersect_key($map, array_flip($keys));
416
417
                foreach ($filtered as $keys2) {
418
                    foreach (array_keys($keys2) as $key2) {
419
                        $buckets[$key2][] = $model;
420 12
                    }
421
                }
422 127
            }
423
        } else {
424
            foreach ($models as $model) {
425
                $keys = $this->getModelKeys($model, $linkKeys);
426
427
                foreach ($keys as $key) {
428
                    $buckets[$key][] = $model;
429 127
                }
430 72
            }
431 72
        }
432 72
433 72
        if (!$this->multiple) {
434
            return array_combine(
435 72
                array_keys($buckets),
436 71
                array_column($buckets, 0)
437 71
            );
438 71
        }
439
440
        return $buckets;
441 72
    }
442
443 72
    private function mapVia(array $map, array $viaMap): array
444 72
    {
445 5
        $resultMap = [];
446 5
447
        foreach ($map as $key => $linkKeys) {
448 5
            $resultMap[$key] = [];
449
            foreach (array_keys($linkKeys) as $linkKey) {
450
                /** @psalm-suppress InvalidArrayOffset */
451
                $resultMap[$key] += $viaMap[$linkKey];
452 127
            }
453 127
        }
454
455 127
        return $resultMap;
456 72
    }
457 71
458 71
    /**
459 71
     * Indexes buckets by a column name.
460 71
     *
461
     * @param Closure|string $indexBy the name of the column by which the query results should be indexed by. This can
462
     * also be a {@see Closure} that returns the index value based on the given models data.
463
     */
464
    private function indexBuckets(array $buckets, Closure|string $indexBy): array
465 127
    {
466 127
        foreach ($buckets as &$models) {
467 127
            $models = ArArrayHelper::index($models, $indexBy);
468
        }
469
470
        return $buckets;
471 127
    }
472 56
473 56
    /**
474
     * @param array $attributes the attributes to prefix.
475
     *
476
     * @throws \Yiisoft\Definitions\Exception\InvalidConfigException
477 127
     */
478
    private function prefixKeyColumns(array $attributes): array
479
    {
480 5
        if (!empty($this->join) || !empty($this->joinWith)) {
481
            if (empty($this->from)) {
482 5
                $alias = $this->getARInstance()->getTableName();
483
            } else {
484 5
                foreach ($this->from as $alias => $table) {
485 5
                    if (!is_string($alias)) {
486 5
                        $alias = $table;
487
                    }
488
                    break;
489
                }
490 5
            }
491
492
            if (isset($alias)) {
493
                foreach ($attributes as $i => $attribute) {
494
                    $attributes[$i] = "$alias.$attribute";
495
                }
496
            }
497
        }
498
499
        return $attributes;
500
    }
501
502 17
    /**
503
     * @throws \Yiisoft\Definitions\Exception\InvalidConfigException
504 17
     */
505
    protected function filterByModels(array $models): void
506 17
    {
507 17
        $attributes = array_keys($this->link);
508 17
        $attributes = $this->prefixKeyColumns($attributes);
509 17
510 17
        $model = reset($models);
511
        $values = [];
512
513
        if (count($attributes) === 1) {
514 17
            /** single key */
515
            $attribute = reset($this->link);
516
517
            if ($model instanceof ActiveRecordInterface) {
518
                foreach ($models as $model) {
519
                    $value = $model->getAttribute($attribute);
520
521
                    if ($value !== null) {
522 236
                        if (is_array($value)) {
523
                            $values = [...$values, ...$value];
524 236
                        } else {
525 36
                            $values[] = $value;
526 12
                        }
527
                    }
528 32
                }
529 32
            } else {
530 32
                foreach ($models as $model) {
531
                    if (isset($model[$attribute])) {
532 32
                        $value = $model[$attribute];
533
534
                        if (is_array($value)) {
535
                            $values = [...$values, ...$value];
536 36
                        } else {
537 36
                            $values[] = $value;
538 36
                        }
539
                    }
540
                }
541
            }
542
543 236
            if (!empty($values)) {
544
                $scalarValues = array_filter($values, 'is_scalar');
545
                $nonScalarValues = array_diff_key($values, $scalarValues);
546 236
547
                $scalarValues = array_unique($scalarValues);
548 236
                $values = [...$scalarValues, ...$nonScalarValues];
549
            }
550 236
        } else {
551
            $nulls = array_fill_keys($this->link, null);
552 236
553 236
            if ($model instanceof ActiveRecordInterface) {
554
                foreach ($models as $model) {
555 232
                    $value = $model->getAttributes($this->link);
556 232
557 232
                    if (!empty($value)) {
558 228
                        $values[] = array_combine($attributes, array_merge($nulls, $value));
559
                    }
560 228
                }
561
            } else {
562
                foreach ($models as $model) {
563 228
                    $value = array_intersect_key($model, $nulls);
564
565
                    if (!empty($value)) {
566
                        $values[] = array_combine($attributes, array_merge($nulls, $value));
567
                    }
568 232
                }
569 232
            }
570
        }
571
572
        if (empty($values)) {
573
            $this->emulateExecution();
0 ignored issues
show
Bug introduced by
It seems like emulateExecution() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

573
            $this->/** @scrutinizer ignore-call */ 
574
                   emulateExecution();
Loading history...
574
            $this->andWhere('1=0');
0 ignored issues
show
Bug introduced by
It seems like andWhere() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

574
            $this->/** @scrutinizer ignore-call */ 
575
                   andWhere('1=0');
Loading history...
575 8
            return;
576
        }
577 8
578 8
        $this->andWhere(['in', $attributes, $values]);
579
    }
580 8
581 8
    private function getModelKeys(ActiveRecordInterface|array $activeRecord, array $attributes): array
582
    {
583
        $key = [];
584 8
585
        if (is_array($activeRecord)) {
0 ignored issues
show
introduced by
The condition is_array($activeRecord) is always true.
Loading history...
586 8
            foreach ($attributes as $attribute) {
587
                if (isset($activeRecord[$attribute])) {
588
                    $key[] = is_array($activeRecord[$attribute])
589
                        ? $activeRecord[$attribute]
590
                        : (string) $activeRecord[$attribute];
591
                }
592 236
            }
593 232
        } else {
594 232
            foreach ($attributes as $attribute) {
595 232
                $value = $activeRecord->getAttribute($attribute);
596 232
597 228
                if ($value !== null) {
598
                    $key[] = is_array($value)
599 8
                        ? $value
600
                        : (string) $value;
601
                }
602
            }
603 232
        }
604 232
605
        return match (count($key)) {
606
            0 => [],
607 236
            1 => is_array($key[0]) ? $key[0] : [$key[0]],
608 236
            default => [serialize($key)],
609
        };
610
    }
611
612
    /**
613
     * @param array $primaryModels either array of AR instances or arrays.
614
     *
615
     * @throws Exception
616 127
     * @throws Throwable
617
     * @throws \Yiisoft\Definitions\Exception\InvalidConfigException
618 127
     */
619
    private function findJunctionRows(array $primaryModels): array
620 127
    {
621 127
        if (empty($primaryModels)) {
622
            return [];
623
        }
624 127
625
        $this->filterByModels($primaryModels);
626
627
        /* @var $primaryModel ActiveRecord */
628 127
        $primaryModel = reset($primaryModels);
629
630 127
        if (!$primaryModel instanceof ActiveRecordInterface) {
0 ignored issues
show
introduced by
$primaryModel is always a sub-type of Yiisoft\ActiveRecord\ActiveRecordInterface.
Loading history...
631
            /** when primaryModels are array of arrays (asArray case) */
632
            $primaryModel = $this->arClass;
0 ignored issues
show
Unused Code introduced by
The assignment to $primaryModel is dead and can be removed.
Loading history...
633
        }
634
635
        return $this->asArray()->all();
0 ignored issues
show
Bug introduced by
It seems like asArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

635
        return $this->/** @scrutinizer ignore-call */ asArray()->all();
Loading history...
636
    }
637
638 127
    public function getMultiple(): bool
639
    {
640 127
        return $this->multiple;
641
    }
642
643
    /**
644
     * @return ActiveRecordInterface|null the primary model of a relational query.
645
     *
646
     * This is used only in lazy loading with dynamic query options.
647
     */
648 127
    public function getPrimaryModel(): ActiveRecordInterface|null
649
    {
650
        return $this->primaryModel;
651
    }
652
653
    /**
654
     * @psalm-return string[]
655
     */
656 28
    public function getLink(): array
657
    {
658 28
        return $this->link;
659
    }
660
661
    public function getVia(): array|ActiveQueryInterface|null
662 28
    {
663
        return $this->via;
664
    }
665 28
666
    public function multiple(bool $value): self
667 28
    {
668
        $this->multiple = $value;
669
670
        return $this;
671
    }
672 28
673
    public function primaryModel(ActiveRecordInterface $value): self
674
    {
675
        $this->primaryModel = $value;
676
677
        return $this;
678
    }
679
680
    public function link(array $value): self
681
    {
682 39
        $this->link = $value;
683
684 39
        return $this;
685
    }
686
}
687