@@ -33,7 +33,13 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
33
33
*/
34
34
protected function hydratePivotRelation (array $ models )
35
35
{
36
- // Do nothing.
36
+ foreach ($ models as $ model ) {
37
+ $ keyToUse = $ this ->getTable () == $ model ->getTable () ? $ this ->getForeignKey () : $ this ->getRelatedKey ();
38
+ $ pcontent = $ model ->getAttributes ()[$ keyToUse ];
39
+ $ model ->setRelation ($ this ->accessor , $ this ->newExistingPivot (
40
+ is_string ($ pcontent [0 ]) ? ['_id ' => $ pcontent ] : $ pcontent [0 ]
41
+ ));
42
+ }
37
43
}
38
44
39
45
/**
@@ -71,8 +77,10 @@ public function addConstraints()
71
77
protected function setWhere ()
72
78
{
73
79
$ foreign = $ this ->getForeignKey ();
74
-
75
- $ this ->query ->where ($ foreign , '= ' , $ this ->parent ->getKey ());
80
+ $ key = $ this ->parent ->getKey ();
81
+ $ this ->query
82
+ ->where ($ foreign , '= ' , $ key )
83
+ ->orWhereRaw ([$ foreign .'._id ' => $ key ]);
76
84
77
85
return $ this ;
78
86
}
@@ -129,6 +137,12 @@ public function sync($ids, $detaching = true)
129
137
// See issue #256.
130
138
if ($ current instanceof Collection) {
131
139
$ current = $ ids ->modelKeys ();
140
+ } elseif (is_array ($ current )) {
141
+ foreach ($ current as $ key => $ value ) {
142
+ if (is_array ($ value ) && $ value ['_id ' ]) {
143
+ $ current [$ key ] = $ value ['_id ' ];
144
+ }
145
+ }
132
146
}
133
147
134
148
$ records = $ this ->formatSyncList ($ ids );
@@ -171,7 +185,30 @@ public function sync($ids, $detaching = true)
171
185
*/
172
186
public function updateExistingPivot ($ id , array $ attributes , $ touch = true )
173
187
{
174
- // Do nothing, we have no pivot table.
188
+ if ($ id instanceof Model) {
189
+ $ model = $ id ;
190
+ $ id = $ model ->getKey ();
191
+ } else {
192
+ if ($ id instanceof Collection) {
193
+ $ id = $ id ->modelKeys ();
194
+ }
195
+
196
+ $ related = $ this ->newRelatedQuery ()->whereIn ($ this ->related ->getKeyName (), (array ) $ id );
197
+ $ filter = [$ this ->parentKey => $ this ->parent ->getKey ()];
198
+ $ pivot_x = [array_merge ($ attributes , $ filter )];
199
+
200
+ //TODO: Put this in a transaction
201
+ $ related ->pull ($ this ->getForeignKey (), $ this ->parent ->getKey ());
202
+ $ related ->pull ($ this ->getForeignKey (), $ filter );
203
+ $ related ->push ($ this ->getForeignKey (), $ pivot_x , true );
204
+ }
205
+ $ filter = [$ this ->parentKey => $ id ];
206
+ $ pivot_x = [array_merge ($ attributes , $ filter )];
207
+
208
+ //TODO: Put this in a transaction
209
+ $ this ->parent ->pull ($ this ->getRelatedKey (), $ id );
210
+ $ this ->parent ->pull ($ this ->getRelatedKey (), $ filter );
211
+ $ this ->parent ->push ($ this ->getRelatedKey (), $ pivot_x , true );
175
212
}
176
213
177
214
/**
@@ -185,22 +222,30 @@ public function attach($id, array $attributes = [], $touch = true)
185
222
$ id = $ model ->getKey ();
186
223
187
224
// Attach the new parent id to the related model.
188
- $ model ->push ($ this ->foreignPivotKey , $ this ->parent ->getKey (), true );
225
+ $ model ->push ($ this ->foreignPivotKey , [ array_merge ( $ attributes , [ ' _id ' => $ this ->parent ->getKey ()])] , true );
189
226
} else {
190
227
if ($ id instanceof Collection) {
191
228
$ id = $ id ->modelKeys ();
192
229
}
193
230
194
231
$ query = $ this ->newRelatedQuery ();
195
232
196
- $ query ->whereIn ($ this ->related ->getKeyName (), (array ) $ id );
233
+ $ query
234
+ ->whereIn ($ this ->related ->getKeyName (), (array ) $ id )
235
+ ->orWhereIn ($ this ->related ->getKeyName ().'._id ' , (array ) $ id );
197
236
198
237
// Attach the new parent id to the related model.
199
- $ query ->push ($ this ->foreignPivotKey , $ this ->parent ->getKey (), true );
238
+ $ query ->push ($ this ->foreignPivotKey , [array_merge ($ attributes , ['_id ' => $ this ->parent ->getKey ()])], true );
239
+ }
240
+
241
+ //Pivot Collection
242
+ $ pivot_x = [];
243
+ foreach ((array ) $ id as $ item ) {
244
+ $ pivot_x [] = array_merge ($ attributes , ['_id ' => $ item ]);
200
245
}
201
246
202
247
// Attach the new ids to the parent model.
203
- $ this ->parent ->push ($ this ->getRelatedKey (), ( array ) $ id , true );
248
+ $ this ->parent ->push ($ this ->getRelatedKey (), $ pivot_x , true );
204
249
205
250
if ($ touch ) {
206
251
$ this ->touchIfTouching ();
@@ -224,7 +269,9 @@ public function detach($ids = [], $touch = true)
224
269
$ ids = (array ) $ ids ;
225
270
226
271
// Detach all ids from the parent model.
272
+ // Legacy Support
227
273
$ this ->parent ->pull ($ this ->getRelatedKey (), $ ids );
274
+ $ this ->parent ->pull ($ this ->getRelatedKey (), ['_id ' => ['$in ' => $ ids ]]);
228
275
229
276
// Prepare the query to select all related objects.
230
277
if (count ($ ids ) > 0 ) {
@@ -233,6 +280,7 @@ public function detach($ids = [], $touch = true)
233
280
234
281
// Remove the relation to the parent.
235
282
$ query ->pull ($ this ->foreignPivotKey , $ this ->parent ->getKey ());
283
+ $ query ->pull ($ this ->foreignPivotKey , [$ this ->parentKey => $ this ->parent ->getKey ()]);
236
284
237
285
if ($ touch ) {
238
286
$ this ->touchIfTouching ();
@@ -253,10 +301,14 @@ protected function buildDictionary(Collection $results)
253
301
// parents without having a possibly slow inner loops for every models.
254
302
$ dictionary = [];
255
303
256
- foreach ($ results as $ result ) {
257
- foreach ($ result ->$ foreign as $ item ) {
258
- $ dictionary [$ item ][] = $ result ;
259
- }
304
+ foreach ($ results as $ result ) {
305
+ foreach ($ result ->$ foreign as $ item ) {
306
+ if (is_array ($ item )) {
307
+ $ dictionary [$ item ['_id ' ]][] = $ result ;
308
+ } else {
309
+ $ dictionary [$ item ][] = $ result ;
310
+ }
311
+ }
260
312
}
261
313
262
314
return $ dictionary ;
@@ -342,4 +394,15 @@ protected function whereInMethod(EloquentModel $model, $key)
342
394
{
343
395
return 'whereIn ' ;
344
396
}
397
+
398
+ /**
399
+ * @inheritDoc
400
+ */
401
+ public function addEagerConstraints (array $ models )
402
+ {
403
+ $ keys = $ this ->getKeys ($ models , $ this ->parentKey );
404
+ $ this ->query
405
+ ->whereIn ($ this ->getQualifiedForeignPivotKeyName (), $ keys )
406
+ ->orWhereRaw ([$ this ->getQualifiedForeignPivotKeyName ().'._id ' => ['$in ' => $ keys ]]);
407
+ }
345
408
}
0 commit comments