-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBelongsToMany.php
462 lines (384 loc) · 13.8 KB
/
BelongsToMany.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Relations;
use BackedEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId;
use function array_diff;
use function array_intersect;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
use function collect;
use function count;
use function in_array;
use function is_numeric;
class BelongsToMany extends EloquentBelongsToMany
{
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getHasCompareKey()
{
return $this->getForeignKey();
}
/** @inheritdoc */
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
return $query;
}
/** @inheritdoc */
protected function hydratePivotRelation(array $models)
{
// Do nothing.
}
/**
* Set the select clause for the relation query.
*
* @return array
*/
protected function getSelectColumns(array $columns = ['*'])
{
return $columns;
}
/** @inheritdoc */
protected function shouldSelect(array $columns = ['*'])
{
return $columns;
}
/** @inheritdoc */
public function addConstraints()
{
if (static::$constraints) {
$this->setWhere();
}
}
/**
* Set the where clause for the relation query.
*
* @return $this
*/
protected function setWhere()
{
$foreign = $this->getForeignKey();
$this->query->where($foreign, '=', $this->parent->{$this->parentKey});
return $this;
}
/** @inheritdoc */
public function save(Model $model, array $pivotAttributes = [], $touch = true)
{
$model->save(['touch' => false]);
$this->attach($model, $pivotAttributes, $touch);
return $model;
}
/** @inheritdoc */
public function create(array $attributes = [], array $joining = [], $touch = true)
{
$instance = $this->related->newInstance($attributes);
// Once we save the related model, we need to attach it to the base model via
// through intermediate table so we'll use the existing "attach" method to
// accomplish this which will insert the record and any more attributes.
$instance->save(['touch' => false]);
$this->attach($instance, $joining, $touch);
return $instance;
}
/**
* Format the sync / toggle record list so that it is keyed by ID.
*
* @param array $records
*
* @return array
*/
protected function formatRecordsList($records): array
{
//Support for an object type id.
//Removal of attribute management because there is no pivot table
return collect($records)->map(function ($id) {
if ($id instanceof BackedEnum) {
$id = $id->value;
}
return $id;
})->all();
}
/**
* Toggles a model (or models) from the parent.
*
* Each existing model is detached, and non existing ones are attached.
*
* @param mixed $ids
* @param bool $touch
*
* @return array
*/
public function toggle($ids, $touch = true)
{
$changes = [
'attached' => [],
'detached' => [],
'updated' => [],
];
if ($ids instanceof Collection) {
$ids = $this->parseIds($ids);
} elseif ($ids instanceof Model) {
$ids = $this->parseIds($ids);
}
// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = match ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
true => $this->parent->{$this->relatedPivotKey} ?: [],
false => $this->parent->{$this->relationName} ?: [],
};
// Support Base Collection
if ($current instanceof BaseCollection) {
$current = $this->parseIds($current);
}
$current = Arr::wrap($current);
$records = $this->formatRecordsList($ids);
$detach = array_values(array_intersect($current, $records));
if (count($detach) > 0) {
$this->detach($detach, false);
$changes['detached'] = (array) array_map(function ($v) {
return is_numeric($v) ? (int) $v : (string) $v;
}, $detach);
}
// Finally, for all of the records which were not "detached", we'll attach the
// records into the intermediate table. Then, we will add those attaches to
// this change list and get ready to return these results to the callers.
$attach = array_values(array_diff($records, $current));
if (count($attach) > 0) {
$this->attach($attach, [], false);
$changes['attached'] = (array) array_map(function ($v) {
return $this->castKey($v);
}, $attach);
}
// Once we have finished attaching or detaching the records, we will see if we
// have done any attaching or detaching, and if we have we will touch these
// relationships if they are configured to touch on any database updates.
if (
$touch && (count($changes['attached']) ||
count($changes['detached']))
) {
$this->parent->touch();
$this->newRelatedQuery()->whereIn($this->relatedKey, $ids)->touch();
}
return $changes;
}
/**
* Sync the intermediate tables with a list of IDs or collection of models.
*
* @param \Illuminate\Support\Collection|Model|array $ids
* @param bool $detaching
*
* @return array
*/
public function sync($ids, $detaching = true)
{
$changes = [
'attached' => [],
'detached' => [],
'updated' => [],
];
if ($ids instanceof Collection) {
$ids = $this->parseIds($ids);
} elseif ($ids instanceof Model) {
$ids = $this->parseIds($ids);
}
// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = match ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
true => $this->parent->{$this->relatedPivotKey} ?: [],
false => $this->parent->{$this->relationName} ?: [],
};
// Support Base Collection
if ($current instanceof BaseCollection) {
$current = $this->parseIds($current);
}
$current = Arr::wrap($current);
$records = $this->formatRecordsList($ids);
$detach = array_values(array_diff($current, $records));
// Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the
// the array of the IDs given to the method which will complete the sync.
if ($detaching && count($detach) > 0) {
$this->detach($detach, false);
$changes['detached'] = (array) array_map(function ($v) {
return is_numeric($v) ? (int) $v : (string) $v;
}, $detach);
}
// Now we are finally ready to attach the new records. Note that we'll disable
// touching until after the entire operation is complete so we don't fire a
// ton of touch operations until we are totally done syncing the records.
foreach ($records as $id) {
// Only non strict check if exist no update s possible beacause no attributtes
if (! in_array($id, $current)) {
$this->attach($id, [], false);
$changes['attached'][] = $this->castKey($id);
}
}
if ((count($changes['attached']) || count($changes['detached']))) {
$touches = array_merge($detach, $records);
$this->parent->touch();
$this->newRelatedQuery()->whereIn($this->relatedKey, $touches)->touch();
}
return $changes;
}
/** @inheritdoc */
public function updateExistingPivot($id, array $attributes, $touch = true)
{
// Do nothing, we have no pivot table.
return $this;
}
/** @inheritdoc */
public function attach($id, array $attributes = [], $touch = true)
{
if ($id instanceof Model) {
$model = $id;
$id = $this->parseId($model);
// Attach the new parent id to the related model.
$model->push($this->foreignPivotKey, $this->parent->{$this->parentKey}, true);
} else {
if ($id instanceof Collection) {
$id = $this->parseIds($id);
}
$query = $this->newRelatedQuery();
$query->whereIn($this->relatedKey, (array) $id);
// Attach the new parent id to the related model.
$query->push($this->foreignPivotKey, $this->parent->{$this->parentKey}, true);
}
// Attach the new ids to the parent model.
if ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
if ($id instanceof ObjectId) {
$id = [$id];
} else {
$id = (array) $id;
}
$this->parent->push($this->relatedPivotKey, $id, true);
} else {
$instance = new $this->related();
$instance->forceFill([$this->relatedKey => $id]);
$relationData = $this->parent->{$this->relationName}->push($instance)->unique($this->relatedKey);
$this->parent->setRelation($this->relationName, $relationData);
}
if (! $touch) {
return;
}
$this->parent->touch();
$this->newRelatedQuery()->whereIn($this->relatedKey, (array) $id);
}
/** @inheritdoc */
public function detach($ids = [], $touch = true)
{
if ($ids instanceof Collection) {
$ids = $this->parseIds($ids);
} elseif ($ids instanceof Model) {
$ids = $this->parseIds($ids);
}
$query = $this->newRelatedQuery();
// If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
if ($ids instanceof ObjectId) {
$ids = [$ids];
} else {
$ids = (array) $ids;
}
// Detach all ids from the parent model.
if ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
$this->parent->pull($this->relatedPivotKey, $ids);
} else {
$value = $this->parent->{$this->relationName}
->filter(fn ($rel) => ! in_array($rel->{$this->relatedKey}, $ids));
$this->parent->setRelation($this->relationName, $value);
}
// Prepare the query to select all related objects.
if (count($ids) > 0) {
$query->whereIn($this->relatedKey, $ids);
}
// Remove the relation to the parent.
assert($this->parent instanceof Model);
assert($query instanceof \MongoDB\Laravel\Eloquent\Builder);
$query->pull($this->foreignPivotKey, $this->parent->{$this->parentKey});
if ($touch) {
$this->parent->touch();
$query->touch();
}
return count($ids);
}
/** @inheritdoc */
protected function buildDictionary(Collection $results)
{
$foreign = $this->foreignPivotKey;
// First we will build a dictionary of child models keyed by the foreign key
// of the relation so that we will easily and quickly match them to their
// parents without having a possibly slow inner loops for every models.
$dictionary = [];
foreach ($results as $result) {
foreach ($result->$foreign as $item) {
//Prevent if id is non keyable
if ($item instanceof ObjectId) {
$item = (string) $item;
}
if ($item instanceof Binary) {
$item = (string) $item->getData();
}
$dictionary[$item][] = $result;
}
}
return $dictionary;
}
/** @inheritdoc */
public function newPivotQuery()
{
return $this->newRelatedQuery();
}
/**
* Create a new query builder for the related model.
*
* @return Builder|Model
*/
public function newRelatedQuery()
{
return $this->related->newQuery();
}
/**
* Get the fully qualified foreign key for the relation.
*
* @return string
*/
public function getForeignKey()
{
return $this->foreignPivotKey;
}
/** @inheritdoc */
public function getQualifiedForeignPivotKeyName()
{
return $this->foreignPivotKey;
}
/** @inheritdoc */
public function getQualifiedRelatedPivotKeyName()
{
return $this->relatedPivotKey;
}
/**
* Get the name of the "where in" method for eager loading.
*
* @param string $key
*
* @return string
*/
protected function whereInMethod(Model $model, $key)
{
return 'whereIn';
}
}