@@ -55,6 +55,10 @@ public function __construct(Builder $query, Model $parent, $localKey, $foreignKe
5555 */
5656 public function addConstraints ()
5757 {
58+ if (static ::$ constraints )
59+ {
60+ $ this ->query ->where ($ this ->parent ->getKeyName (), '= ' , $ this ->parent ->getKey ());
61+ }
5862 }
5963
6064 /**
@@ -180,7 +184,7 @@ protected function performInsert(Model $model)
180184 $ model ->exists = true ;
181185
182186 // 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 );
184188
185189 // Get existing embedded documents.
186190 $ documents = $ this ->getEmbedded ();
@@ -209,13 +213,20 @@ protected function performUpdate(Model $model)
209213 $ model ->setUpdatedAt ($ time );
210214 }
211215
212- $ key = $ model ->getKey ();
216+ // Convert the id to MongoId if necessary.
217+ $ id = $ this ->query ->getQuery ()->convertKey ($ model ->getKey ());
213218
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 ()));
215222
216223 // Get existing embedded documents.
217224 $ documents = $ this ->getEmbedded ();
218225
226+ $ primaryKey = $ this ->related ->getKeyName ();
227+
228+ $ key = $ model ->getKey ();
229+
219230 // Replace the document in the parent model.
220231 foreach ($ documents as $ i => $ document )
221232 {
@@ -228,7 +239,7 @@ protected function performUpdate(Model $model)
228239
229240 $ this ->setEmbedded ($ documents );
230241
231- return $ this -> parent -> save () ? $ model : false ;
242+ return $ result ? $ model : false ;
232243 }
233244
234245 /**
@@ -300,25 +311,33 @@ public function destroy($ids = array())
300311 // We'll return the numbers of affected rows when we do the deletes.
301312 $ ids = (array ) $ ids ;
302313
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+
303327 // Get existing embedded documents.
304328 $ documents = $ this ->getEmbedded ();
305329
306- $ primaryKey = $ this ->related ->getKeyName ();
307-
308330 // Remove the document from the parent model.
309331 foreach ($ documents as $ i => $ document )
310332 {
311333 if (in_array ($ document [$ primaryKey ], $ ids ))
312334 {
313335 unset($ documents [$ i ]);
314- $ count ++;
315336 }
316337 }
317338
318339 $ this ->setEmbedded ($ documents );
319340
320- $ this ->parent ->save ();
321-
322341 return $ count ;
323342 }
324343
0 commit comments