@@ -146,9 +146,10 @@ public function find($id)
146
146
}
147
147
148
148
$ records = $ this ->getEmbeddedRecords ();
149
-
150
149
$ primaryKey = $ this ->related ->getKeyName ();
151
150
151
+ // Traverse all embedded records and find the first record
152
+ // that matches the given primary key.
152
153
$ record = array_first ($ records , function ($ itemKey , $ record ) use ($ primaryKey , $ id )
153
154
{
154
155
return $ record [$ primaryKey ] == $ id ;
@@ -283,6 +284,7 @@ public function destroy($ids = array())
283
284
{
284
285
$ ids = $ this ->getIdsArrayFrom ($ ids );
285
286
287
+ // Get all models matching the given ids.
286
288
$ models = $ this ->get ()->only ($ ids );
287
289
288
290
// Pull the documents from the database.
@@ -302,21 +304,19 @@ public function dissociate($ids = array())
302
304
{
303
305
$ ids = $ this ->getIdsArrayFrom ($ ids );
304
306
307
+ $ records = $ this ->getEmbeddedRecords ();
305
308
$ primaryKey = $ this ->related ->getKeyName ();
306
309
307
- // Get existing embedded documents.
308
- $ documents = $ this ->getEmbeddedRecords ();
309
-
310
310
// Remove the document from the parent model.
311
- foreach ($ documents as $ i => $ document )
311
+ foreach ($ records as $ i => $ record )
312
312
{
313
- if (in_array ($ document [$ primaryKey ], $ ids ))
313
+ if (in_array ($ record [$ primaryKey ], $ ids ))
314
314
{
315
- unset($ documents [$ i ]);
315
+ unset($ records [$ i ]);
316
316
}
317
317
}
318
318
319
- $ this ->setEmbeddedRecords ($ documents );
319
+ $ this ->setEmbeddedRecords ($ records );
320
320
321
321
// We return the total number of deletes for the operation. The developers
322
322
// can then check this number as a boolean type value or get this total count
@@ -325,7 +325,32 @@ public function dissociate($ids = array())
325
325
}
326
326
327
327
/**
328
- * Delete alias.
328
+ * Delete all embedded models.
329
+ *
330
+ * @return int
331
+ */
332
+ public function delete ()
333
+ {
334
+ // Overwrite the local key with an empty array.
335
+ $ result = $ this ->query ->update (array ($ this ->localKey => array ()));
336
+
337
+ // If the update query was successful, we will remove the embedded records
338
+ // of the parent instance.
339
+ if ($ result )
340
+ {
341
+ $ count = $ this ->count ();
342
+
343
+ $ this ->setEmbeddedRecords (array ());
344
+
345
+ // Return the number of deleted embedded records.
346
+ return $ count ;
347
+ }
348
+
349
+ return $ result ;
350
+ }
351
+
352
+ /**
353
+ * Destroy alias.
329
354
*
330
355
* @param mixed $ids
331
356
* @return int
@@ -356,14 +381,20 @@ protected function performInsert(Model $model)
356
381
{
357
382
if ($ this ->fireModelEvent ($ model , 'creating ' ) === false ) return false ;
358
383
359
- // Associate the new model to the parent.
360
- $ this ->associateNew ($ model );
384
+ // Create a new key if needed.
385
+ if ( ! $ model ->getAttribute ('_id ' ))
386
+ {
387
+ $ model ->setAttribute ('_id ' , new MongoId );
388
+ }
361
389
362
390
// Push the new model to the database.
363
391
$ result = $ this ->query ->push ($ this ->localKey , $ model ->getAttributes (), true );
364
392
365
393
if ($ result )
366
394
{
395
+ // Associate the new model to the parent.
396
+ $ this ->associateNew ($ model );
397
+
367
398
$ this ->fireModelEvent ($ model , 'created ' , false );
368
399
369
400
return $ model ;
@@ -382,9 +413,6 @@ protected function performUpdate(Model $model)
382
413
{
383
414
if ($ this ->fireModelEvent ($ model , 'updating ' ) === false ) return false ;
384
415
385
- // Update the related model in the parent instance
386
- $ this ->associateExisting ($ model );
387
-
388
416
// Get the correct foreign key value.
389
417
$ id = $ this ->getForeignKeyValue ($ model );
390
418
@@ -394,6 +422,9 @@ protected function performUpdate(Model $model)
394
422
395
423
if ($ result )
396
424
{
425
+ // Update the related model in the parent instance
426
+ $ this ->associateExisting ($ model );
427
+
397
428
$ this ->fireModelEvent ($ model , 'updated ' , false );
398
429
399
430
return $ model ;
@@ -438,18 +469,18 @@ protected function performDelete(Model $model)
438
469
*/
439
470
protected function associateNew ($ model )
440
471
{
441
- // Create a new key.
472
+ // Create a new key if needed .
442
473
if ( ! $ model ->getAttribute ('_id ' ))
443
474
{
444
475
$ model ->setAttribute ('_id ' , new MongoId );
445
476
}
446
477
447
- $ documents = $ this ->getEmbeddedRecords ();
478
+ $ records = $ this ->getEmbeddedRecords ();
448
479
449
480
// Add the document to the parent model.
450
- $ documents [] = $ model ->getAttributes ();
481
+ $ records [] = $ model ->getAttributes ();
451
482
452
- $ this ->setEmbeddedRecords ($ documents );
483
+ $ this ->setEmbeddedRecords ($ records );
453
484
454
485
// Mark the model as existing.
455
486
$ model ->exists = true ;
@@ -466,23 +497,22 @@ protected function associateNew($model)
466
497
protected function associateExisting ($ model )
467
498
{
468
499
// Get existing embedded documents.
469
- $ documents = $ this ->getEmbeddedRecords ();
500
+ $ records = $ this ->getEmbeddedRecords ();
470
501
471
502
$ primaryKey = $ this ->related ->getKeyName ();
472
-
473
503
$ key = $ model ->getKey ();
474
504
475
505
// Replace the document in the parent model.
476
- foreach ($ documents as &$ document )
506
+ foreach ($ records as &$ record )
477
507
{
478
- if ($ document [$ primaryKey ] == $ key )
508
+ if ($ record [$ primaryKey ] == $ key )
479
509
{
480
- $ document = $ model ->getAttributes ();
510
+ $ record = $ model ->getAttributes ();
481
511
break ;
482
512
}
483
513
}
484
514
485
- $ this ->setEmbeddedRecords ($ documents );
515
+ $ this ->setEmbeddedRecords ($ records );
486
516
487
517
return $ model ;
488
518
}
0 commit comments