@@ -55,6 +55,10 @@ public function __construct(Builder $query, Model $parent, $localKey, $foreignKe
55
55
*/
56
56
public function addConstraints ()
57
57
{
58
+ if (static ::$ constraints )
59
+ {
60
+ $ this ->query ->where ($ this ->parent ->getKeyName (), '= ' , $ this ->parent ->getKey ());
61
+ }
58
62
}
59
63
60
64
/**
@@ -180,7 +184,7 @@ protected function performInsert(Model $model)
180
184
$ model ->exists = true ;
181
185
182
186
// Push the document to the database.
183
- $ result = $ this ->parent ->push ($ this ->localKey , $ model ->getAttributes (), true );
187
+ $ result = $ this ->query ->push ($ this ->localKey , $ model ->getAttributes (), true );
184
188
185
189
// Get existing embedded documents.
186
190
$ documents = $ this ->getEmbedded ();
@@ -209,13 +213,20 @@ protected function performUpdate(Model $model)
209
213
$ model ->setUpdatedAt ($ time );
210
214
}
211
215
212
- $ key = $ model ->getKey ();
216
+ // Convert the id to MongoId if necessary.
217
+ $ id = $ this ->query ->getQuery ()->convertKey ($ model ->getKey ());
213
218
214
- $ primaryKey = $ model ->getKeyName ();
219
+ // Update document in database.
220
+ $ result = $ this ->query ->where ($ this ->localKey . '. ' . $ model ->getKeyName (), $ id )
221
+ ->update (array ($ this ->localKey . '.$ ' => $ model ->getAttributes ()));
215
222
216
223
// Get existing embedded documents.
217
224
$ documents = $ this ->getEmbedded ();
218
225
226
+ $ primaryKey = $ this ->related ->getKeyName ();
227
+
228
+ $ key = $ model ->getKey ();
229
+
219
230
// Replace the document in the parent model.
220
231
foreach ($ documents as $ i => $ document )
221
232
{
@@ -228,7 +239,7 @@ protected function performUpdate(Model $model)
228
239
229
240
$ this ->setEmbedded ($ documents );
230
241
231
- return $ this -> parent -> save () ? $ model : false ;
242
+ return $ result ? $ model : false ;
232
243
}
233
244
234
245
/**
@@ -300,25 +311,33 @@ public function destroy($ids = array())
300
311
// We'll return the numbers of affected rows when we do the deletes.
301
312
$ ids = (array ) $ ids ;
302
313
314
+ $ primaryKey = $ this ->related ->getKeyName ();
315
+
316
+ // Pull the documents from the database.
317
+ foreach ($ ids as $ id )
318
+ {
319
+ // Convert the id to MongoId if necessary.
320
+ $ id = $ this ->query ->getQuery ()->convertKey ($ id );
321
+
322
+ $ this ->query ->pull ($ this ->localKey , array ($ primaryKey => $ id ));
323
+
324
+ $ count ++;
325
+ }
326
+
303
327
// Get existing embedded documents.
304
328
$ documents = $ this ->getEmbedded ();
305
329
306
- $ primaryKey = $ this ->related ->getKeyName ();
307
-
308
330
// Remove the document from the parent model.
309
331
foreach ($ documents as $ i => $ document )
310
332
{
311
333
if (in_array ($ document [$ primaryKey ], $ ids ))
312
334
{
313
335
unset($ documents [$ i ]);
314
- $ count ++;
315
336
}
316
337
}
317
338
318
339
$ this ->setEmbedded ($ documents );
319
340
320
- $ this ->parent ->save ();
321
-
322
341
return $ count ;
323
342
}
324
343
0 commit comments