|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Relations\Traits\Concatenation; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Database\Query\JoinClause; |
|
8
|
|
|
|
|
9
|
|
|
trait IsConcatenableBelongsToJsonRelation |
|
10
|
|
|
{ |
|
11
|
|
|
use IsConcatenableRelation; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Append the relation's through parents, foreign and local keys to a deep relationship. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \Illuminate\Database\Eloquent\Model[] $through |
|
17
|
|
|
* @param array $foreignKeys |
|
18
|
|
|
* @param array $localKeys |
|
19
|
|
|
* @param int $position |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
71 |
|
public function appendToDeepRelationship(array $through, array $foreignKeys, array $localKeys, int $position): array |
|
23
|
|
|
{ |
|
24
|
71 |
|
if ($position === 0) { |
|
25
|
23 |
|
$foreignKeys[] = $this->ownerKey; |
|
26
|
|
|
|
|
27
|
23 |
|
$localKeys[] = function (Builder $query, Builder $parentQuery = null) { |
|
28
|
11 |
|
if ($parentQuery) { |
|
29
|
5 |
|
$this->getRelationExistenceQuery($this->query, $parentQuery); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
11 |
|
$this->mergeWhereConstraints($query, $this->query); |
|
33
|
|
|
}; |
|
34
|
|
|
} else { |
|
35
|
48 |
|
$foreignKeys[] = function (Builder $query, JoinClause $join) { |
|
36
|
48 |
|
$ownerKey = $this->query->qualifyColumn($this->ownerKey); |
|
37
|
|
|
|
|
38
|
48 |
|
[$sql, $bindings] = $this->relationExistenceQueryOwnerKey($query, $ownerKey); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
48 |
|
$query->addBinding($bindings, 'join'); |
|
41
|
|
|
|
|
42
|
48 |
|
$join->whereJsonContains( |
|
43
|
48 |
|
$this->getQualifiedPath(), |
|
|
|
|
|
|
44
|
48 |
|
$query->getQuery()->connection->raw($sql) |
|
45
|
|
|
); |
|
46
|
|
|
}; |
|
47
|
|
|
|
|
48
|
48 |
|
$localKeys[] = null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
71 |
|
return [$through, $foreignKeys, $localKeys]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Match the eagerly loaded results for a deep relationship to their parents. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $models |
|
58
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
|
59
|
|
|
* @param string $relation |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
12 |
|
public function matchResultsForDeepRelationship(array $models, Collection $results, string $relation): array |
|
63
|
|
|
{ |
|
64
|
12 |
|
$dictionary = $this->buildDictionaryForDeepRelationship($results); |
|
65
|
|
|
|
|
66
|
12 |
|
foreach ($models as $model) { |
|
67
|
12 |
|
$matches = []; |
|
68
|
|
|
|
|
69
|
12 |
|
foreach ($this->getForeignKeys($model) as $id) { |
|
|
|
|
|
|
70
|
12 |
|
if (isset($dictionary[$id])) { |
|
71
|
12 |
|
$matches = array_merge($matches, $dictionary[$id]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
12 |
|
$collection = $this->related->newCollection($matches); |
|
76
|
|
|
|
|
77
|
12 |
|
$model->setRelation($relation, $collection); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
12 |
|
return $models; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Build the model dictionary for a deep relation. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
12 |
|
protected function buildDictionaryForDeepRelationship(Collection $results): array |
|
90
|
|
|
{ |
|
91
|
12 |
|
$dictionary = []; |
|
92
|
|
|
|
|
93
|
12 |
|
foreach ($results as $result) { |
|
94
|
12 |
|
$dictionary[$result->laravel_through_key][] = $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
12 |
|
return $dictionary; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|