|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BiiiiiigMonster\Hasin\Database\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
15
|
|
|
use LogicException; |
|
16
|
|
|
|
|
17
|
|
|
class RelationMixin |
|
18
|
|
|
{ |
|
19
|
|
|
public function getRelationExistenceInQuery(): Closure |
|
20
|
|
|
{ |
|
21
|
|
|
return function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder { |
|
|
|
|
|
|
22
|
|
|
$relation = function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder { |
|
23
|
|
|
return $query->select($columns); |
|
|
|
|
|
|
24
|
|
|
}; |
|
25
|
|
|
// basic builder |
|
26
|
|
|
$belongsTo = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
|
27
|
|
|
$columns = $query->qualifyColumn($this->ownerKey); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$relationQuery = $relation($query, $parentQuery, $columns); |
|
30
|
|
|
|
|
31
|
|
|
if ($parentQuery->getQuery()->from == $query->getQuery()->from) { |
|
|
|
|
|
|
32
|
|
|
$relationQuery->from( |
|
33
|
|
|
$query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash() |
|
|
|
|
|
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$relationQuery->getModel()->setTable($hash); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $relationQuery; |
|
40
|
|
|
}; |
|
41
|
|
|
$belongsToMany = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
|
42
|
|
|
$columns = $this->getExistenceCompareKey(); |
|
|
|
|
|
|
43
|
|
|
if ($parentQuery->getQuery()->from == $query->getQuery()->from) { |
|
44
|
|
|
$query->select($columns); |
|
45
|
|
|
|
|
46
|
|
|
$query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash()); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$this->related->setTable($hash); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->performJoin($query); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
return $relation($query, $parentQuery, $columns); |
|
54
|
|
|
}; |
|
55
|
|
|
$hasOneOrMany = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
|
56
|
|
|
$columns = $this->getExistenceCompareKey(); |
|
57
|
|
|
if ($query->getQuery()->from == $parentQuery->getQuery()->from) { |
|
58
|
|
|
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); |
|
59
|
|
|
|
|
60
|
|
|
$query->getModel()->setTable($hash); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $relation($query, $parentQuery, $columns); |
|
64
|
|
|
}; |
|
65
|
|
|
$hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder { |
|
66
|
|
|
$columns = $this->getQualifiedFirstKeyName(); |
|
|
|
|
|
|
67
|
|
|
if ($parentQuery->getQuery()->from === $query->getQuery()->from) { |
|
68
|
|
|
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); |
|
69
|
|
|
|
|
70
|
|
|
$query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if ($this->throughParentSoftDeletes()) { |
|
|
|
|
|
|
73
|
|
|
$query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$query->getModel()->setTable($hash); |
|
77
|
|
|
|
|
78
|
|
|
return $relation($query, $parentQuery, $columns); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) { |
|
82
|
|
|
$table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash(); |
|
83
|
|
|
|
|
84
|
|
|
$query->join($table, $hash.'.'.$this->secondLocalKey, '=', $this->getQualifiedFarKeyName()); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if ($this->throughParentSoftDeletes()) { |
|
87
|
|
|
$query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $relation($query, $parentQuery, $columns); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->performJoin($query); |
|
94
|
|
|
|
|
95
|
|
|
return $relation($query, $parentQuery, $columns); |
|
96
|
|
|
}; |
|
97
|
|
|
// extended builder |
|
98
|
|
|
$hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { |
|
99
|
|
|
if ($this->isOneOfMany()) { |
|
|
|
|
|
|
100
|
|
|
$this->mergeOneOfManyJoinsTo($query); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $hasOneOrMany($query, $parentQuery); |
|
104
|
|
|
}; |
|
105
|
|
|
$morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder { |
|
106
|
|
|
return $hasOneOrMany($query, $parentQuery)->where( |
|
107
|
|
|
$query->qualifyColumn($this->getMorphType()), |
|
|
|
|
|
|
108
|
|
|
$this->morphClass |
|
|
|
|
|
|
109
|
|
|
); |
|
110
|
|
|
}; |
|
111
|
|
|
$morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder { |
|
112
|
|
|
if ($this->isOneOfMany()) { |
|
113
|
|
|
$this->mergeOneOfManyJoinsTo($query); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $morphOneOrMany($query, $parentQuery); |
|
117
|
|
|
}; |
|
118
|
|
|
$morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder { |
|
119
|
|
|
return $belongsToMany($query, $parentQuery)->where( |
|
120
|
|
|
$this->qualifyPivotColumn($this->morphType), |
|
|
|
|
|
|
121
|
|
|
$this->morphClass |
|
|
|
|
|
|
122
|
|
|
); |
|
123
|
|
|
}; |
|
124
|
|
|
|
|
125
|
|
|
return match (true) { |
|
126
|
|
|
// extended relation |
|
127
|
|
|
$this instanceof HasOne => $hasOne($query, $parentQuery), |
|
128
|
|
|
$this instanceof MorphOne => $morphOne($query, $parentQuery), |
|
129
|
|
|
$this instanceof MorphOneOrMany => $morphOneOrMany($query, $parentQuery), |
|
130
|
|
|
$this instanceof MorphToMany => $morphToMany($query, $parentQuery), |
|
131
|
|
|
// basic relation |
|
132
|
|
|
$this instanceof BelongsTo => $belongsTo($query, $parentQuery), |
|
133
|
|
|
$this instanceof BelongsToMany => $belongsToMany($query, $parentQuery), |
|
134
|
|
|
$this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery), |
|
135
|
|
|
$this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery), |
|
136
|
|
|
default => throw new LogicException( |
|
137
|
|
|
sprintf('%s must be a relationship instance.', $this::class) |
|
138
|
|
|
) |
|
139
|
|
|
}; |
|
140
|
|
|
}; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function getRelationWhereInKey(): Closure |
|
144
|
|
|
{ |
|
145
|
|
|
return fn (): string => match (true) { |
|
146
|
|
|
$this instanceof BelongsTo => $this->getQualifiedForeignKeyName(), |
|
|
|
|
|
|
147
|
|
|
$this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(), |
|
148
|
|
|
$this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(), |
|
|
|
|
|
|
149
|
|
|
default => throw new LogicException( |
|
150
|
|
|
sprintf('%s must be a relationship instance.', $this::class) |
|
151
|
|
|
) |
|
152
|
|
|
}; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.