|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Http\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
16
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
17
|
|
|
use Illuminate\Support\Str; |
|
18
|
|
|
use MaksimM\CompositePrimaryKeys\Eloquent\Relationships\CompositeBelongsTo; |
|
19
|
|
|
|
|
20
|
|
|
trait CompositeRelationships |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param $related |
|
24
|
|
|
* @param null $foreignKey |
|
|
|
|
|
|
25
|
|
|
* @param null $ownerKey |
|
|
|
|
|
|
26
|
|
|
* @param null $relation |
|
|
|
|
|
|
27
|
|
|
* |
|
28
|
|
|
* @return BelongsTo |
|
29
|
|
|
*/ |
|
30
|
|
|
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) |
|
31
|
|
|
{ |
|
32
|
|
|
// If no relation name was given, we will use this debug backtrace to extract |
|
33
|
|
|
// the calling method's name and use that as the relationship name as most |
|
34
|
|
|
// of the time this will be what we desire to use for the relationships. |
|
35
|
|
|
if (is_null($relation)) { |
|
|
|
|
|
|
36
|
|
|
$relation = $this->guessBelongsToRelation(); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Model $instance |
|
41
|
|
|
*/ |
|
42
|
|
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
// If no foreign key was supplied, we can use a backtrace to guess the proper |
|
45
|
|
|
// foreign key name by using the name of the relationship function, which |
|
46
|
|
|
// when combined with an "_id" should conventionally match the columns. |
|
47
|
|
|
if (is_null($foreignKey)) { |
|
|
|
|
|
|
48
|
|
|
$foreignKey = is_array($instance->getKeyName()) ? array_map( |
|
|
|
|
|
|
49
|
|
|
function ($key) use ($relation) { |
|
50
|
|
|
return Str::snake($relation).'_'.$key; |
|
51
|
|
|
}, |
|
52
|
|
|
$instance->getKeyName() |
|
53
|
|
|
) : Str::snake($relation).'_'.$instance->getKeyName(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Once we have the foreign key names, we'll just create a new Eloquent query |
|
57
|
|
|
// for the related models and returns the relationship instance which will |
|
58
|
|
|
// actually be responsible for retrieving and hydrating every relations. |
|
59
|
|
|
$ownerKey = $ownerKey ?: $instance->getKeyName(); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$relationQuery = $this->newBelongsTo( |
|
62
|
|
|
$instance->newQuery(), |
|
63
|
|
|
$this, |
|
|
|
|
|
|
64
|
|
|
$foreignKey, |
|
65
|
|
|
$ownerKey, |
|
66
|
|
|
$relation |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
return $relationQuery; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function executeWithinOptionalBinaryTransformation(Closure $relation, ...$models) |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($models as $model) { |
|
75
|
|
|
if (method_exists($model, 'disableBinaryMutators')) { |
|
76
|
|
|
$model->disableBinaryMutators(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$relationResult = $relation(); |
|
80
|
|
|
foreach ($models as $model) { |
|
81
|
|
|
if (method_exists($model, 'enableBinaryMutators')) { |
|
82
|
|
|
$model->enableBinaryMutators(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $relationResult; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Instantiate a new BelongsTo relationship. |
|
91
|
|
|
* |
|
92
|
|
|
* @param Builder $query |
|
93
|
|
|
* @param Model $child |
|
94
|
|
|
* @param string $foreignKey |
|
95
|
|
|
* @param string $ownerKey |
|
96
|
|
|
* @param string $relation |
|
97
|
|
|
* |
|
98
|
|
|
* @return BelongsTo |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $child, $foreignKey, $ownerKey, $relation) { |
|
103
|
|
|
return new CompositeBelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
|
104
|
|
|
}, $query->getModel(), $child); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Instantiate a new HasOne relationship. |
|
109
|
|
|
* |
|
110
|
|
|
* @param Builder $query |
|
111
|
|
|
* @param Model $parent |
|
112
|
|
|
* @param string $foreignKey |
|
113
|
|
|
* @param string $localKey |
|
114
|
|
|
* |
|
115
|
|
|
* @return HasOne |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $foreignKey, $localKey) { |
|
120
|
|
|
return new HasOne($query, $parent, $foreignKey, $localKey); |
|
121
|
|
|
}, $query->getModel(), $parent); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Instantiate a new MorphOne relationship. |
|
126
|
|
|
* |
|
127
|
|
|
* @param Builder $query |
|
128
|
|
|
* @param Model $parent |
|
129
|
|
|
* @param string $type |
|
130
|
|
|
* @param string $id |
|
131
|
|
|
* @param string $localKey |
|
132
|
|
|
* |
|
133
|
|
|
* @return MorphOne |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $type, $id, $localKey) { |
|
138
|
|
|
return new MorphOne($query, $parent, $type, $id, $localKey); |
|
139
|
|
|
}, $query->getModel(), $parent); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Instantiate a new MorphTo relationship. |
|
144
|
|
|
* |
|
145
|
|
|
* @param Builder $query |
|
146
|
|
|
* @param Model $parent |
|
147
|
|
|
* @param string $foreignKey |
|
148
|
|
|
* @param string $ownerKey |
|
149
|
|
|
* @param string $type |
|
150
|
|
|
* @param string $relation |
|
151
|
|
|
* |
|
152
|
|
|
* @return MorphTo |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $foreignKey, $ownerKey, $type, $relation) { |
|
157
|
|
|
return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); |
|
158
|
|
|
}, $query->getModel(), $parent); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Instantiate a new HasMany relationship. |
|
163
|
|
|
* |
|
164
|
|
|
* @param Builder $query |
|
165
|
|
|
* @param Model $parent |
|
166
|
|
|
* @param string $foreignKey |
|
167
|
|
|
* @param string $localKey |
|
168
|
|
|
* |
|
169
|
|
|
* @return HasMany |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $foreignKey, $localKey) { |
|
174
|
|
|
return new HasMany($query, $parent, $foreignKey, $localKey); |
|
175
|
|
|
}, $query->getModel(), $parent); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Instantiate a new HasManyThrough relationship. |
|
180
|
|
|
* |
|
181
|
|
|
* @param Builder $query |
|
182
|
|
|
* @param Model $farParent |
|
183
|
|
|
* @param Model $throughParent |
|
184
|
|
|
* @param string $firstKey |
|
185
|
|
|
* @param string $secondKey |
|
186
|
|
|
* @param string $localKey |
|
187
|
|
|
* @param string $secondLocalKey |
|
188
|
|
|
* |
|
189
|
|
|
* @return HasManyThrough |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) { |
|
194
|
|
|
return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
|
195
|
|
|
}, $query->getModel(), $farParent); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Instantiate a new MorphMany relationship. |
|
200
|
|
|
* |
|
201
|
|
|
* @param Builder $query |
|
202
|
|
|
* @param Model $parent |
|
203
|
|
|
* @param string $type |
|
204
|
|
|
* @param string $id |
|
205
|
|
|
* @param string $localKey |
|
206
|
|
|
* |
|
207
|
|
|
* @return MorphMany |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $type, $id, $localKey) { |
|
212
|
|
|
return new MorphMany($query, $parent, $type, $id, $localKey); |
|
213
|
|
|
}, $query->getModel(), $parent); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Instantiate a new BelongsToMany relationship. |
|
218
|
|
|
* |
|
219
|
|
|
* @param Builder $query |
|
220
|
|
|
* @param Model $parent |
|
221
|
|
|
* @param string $table |
|
222
|
|
|
* @param string $foreignPivotKey |
|
223
|
|
|
* @param string $relatedPivotKey |
|
224
|
|
|
* @param string $parentKey |
|
225
|
|
|
* @param string $relatedKey |
|
226
|
|
|
* @param string $relationName |
|
227
|
|
|
* |
|
228
|
|
|
* @return BelongsToMany |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, |
|
231
|
|
|
$parentKey, $relatedKey, $relationName = null) |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName) { |
|
234
|
|
|
return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
|
235
|
|
|
}, $query->getModel(), $parent); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Instantiate a new MorphToMany relationship. |
|
240
|
|
|
* |
|
241
|
|
|
* @param Builder $query |
|
242
|
|
|
* @param Model $parent |
|
243
|
|
|
* @param string $name |
|
244
|
|
|
* @param string $table |
|
245
|
|
|
* @param string $foreignPivotKey |
|
246
|
|
|
* @param string $relatedPivotKey |
|
247
|
|
|
* @param string $parentKey |
|
248
|
|
|
* @param string $relatedKey |
|
249
|
|
|
* @param string $relationName |
|
250
|
|
|
* @param bool $inverse |
|
251
|
|
|
* |
|
252
|
|
|
* @return MorphToMany |
|
253
|
|
|
*/ |
|
254
|
|
|
protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, |
|
255
|
|
|
$relatedPivotKey, $parentKey, $relatedKey, |
|
256
|
|
|
$relationName = null, $inverse = false) |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, |
|
259
|
|
|
$relationName, $inverse) { |
|
260
|
|
|
return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, |
|
261
|
|
|
$relationName, $inverse); |
|
262
|
|
|
}, $query->getModel(), $parent); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|