Skip to content

Commit b9e1efe

Browse files
committed
Adding delete method for embedded documents
1 parent 418c3db commit b9e1efe

File tree

2 files changed

+78
-24
lines changed

2 files changed

+78
-24
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ public function find($id)
146146
}
147147

148148
$records = $this->getEmbeddedRecords();
149-
150149
$primaryKey = $this->related->getKeyName();
151150

151+
// Traverse all embedded records and find the first record
152+
// that matches the given primary key.
152153
$record = array_first($records, function($itemKey, $record) use ($primaryKey, $id)
153154
{
154155
return $record[$primaryKey] == $id;
@@ -283,6 +284,7 @@ public function destroy($ids = array())
283284
{
284285
$ids = $this->getIdsArrayFrom($ids);
285286

287+
// Get all models matching the given ids.
286288
$models = $this->get()->only($ids);
287289

288290
// Pull the documents from the database.
@@ -302,21 +304,19 @@ public function dissociate($ids = array())
302304
{
303305
$ids = $this->getIdsArrayFrom($ids);
304306

307+
$records = $this->getEmbeddedRecords();
305308
$primaryKey = $this->related->getKeyName();
306309

307-
// Get existing embedded documents.
308-
$documents = $this->getEmbeddedRecords();
309-
310310
// Remove the document from the parent model.
311-
foreach ($documents as $i => $document)
311+
foreach ($records as $i => $record)
312312
{
313-
if (in_array($document[$primaryKey], $ids))
313+
if (in_array($record[$primaryKey], $ids))
314314
{
315-
unset($documents[$i]);
315+
unset($records[$i]);
316316
}
317317
}
318318

319-
$this->setEmbeddedRecords($documents);
319+
$this->setEmbeddedRecords($records);
320320

321321
// We return the total number of deletes for the operation. The developers
322322
// can then check this number as a boolean type value or get this total count
@@ -325,7 +325,32 @@ public function dissociate($ids = array())
325325
}
326326

327327
/**
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.
329354
*
330355
* @param mixed $ids
331356
* @return int
@@ -356,14 +381,20 @@ protected function performInsert(Model $model)
356381
{
357382
if ($this->fireModelEvent($model, 'creating') === false) return false;
358383

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+
}
361389

362390
// Push the new model to the database.
363391
$result = $this->query->push($this->localKey, $model->getAttributes(), true);
364392

365393
if ($result)
366394
{
395+
// Associate the new model to the parent.
396+
$this->associateNew($model);
397+
367398
$this->fireModelEvent($model, 'created', false);
368399

369400
return $model;
@@ -382,9 +413,6 @@ protected function performUpdate(Model $model)
382413
{
383414
if ($this->fireModelEvent($model, 'updating') === false) return false;
384415

385-
// Update the related model in the parent instance
386-
$this->associateExisting($model);
387-
388416
// Get the correct foreign key value.
389417
$id = $this->getForeignKeyValue($model);
390418

@@ -394,6 +422,9 @@ protected function performUpdate(Model $model)
394422

395423
if ($result)
396424
{
425+
// Update the related model in the parent instance
426+
$this->associateExisting($model);
427+
397428
$this->fireModelEvent($model, 'updated', false);
398429

399430
return $model;
@@ -438,18 +469,18 @@ protected function performDelete(Model $model)
438469
*/
439470
protected function associateNew($model)
440471
{
441-
// Create a new key.
472+
// Create a new key if needed.
442473
if ( ! $model->getAttribute('_id'))
443474
{
444475
$model->setAttribute('_id', new MongoId);
445476
}
446477

447-
$documents = $this->getEmbeddedRecords();
478+
$records = $this->getEmbeddedRecords();
448479

449480
// Add the document to the parent model.
450-
$documents[] = $model->getAttributes();
481+
$records[] = $model->getAttributes();
451482

452-
$this->setEmbeddedRecords($documents);
483+
$this->setEmbeddedRecords($records);
453484

454485
// Mark the model as existing.
455486
$model->exists = true;
@@ -466,23 +497,22 @@ protected function associateNew($model)
466497
protected function associateExisting($model)
467498
{
468499
// Get existing embedded documents.
469-
$documents = $this->getEmbeddedRecords();
500+
$records = $this->getEmbeddedRecords();
470501

471502
$primaryKey = $this->related->getKeyName();
472-
473503
$key = $model->getKey();
474504

475505
// Replace the document in the parent model.
476-
foreach ($documents as &$document)
506+
foreach ($records as &$record)
477507
{
478-
if ($document[$primaryKey] == $key)
508+
if ($record[$primaryKey] == $key)
479509
{
480-
$document = $model->getAttributes();
510+
$record = $model->getAttributes();
481511
break;
482512
}
483513
}
484514

485-
$this->setEmbeddedRecords($documents);
515+
$this->setEmbeddedRecords($records);
486516

487517
return $model;
488518
}

tests/RelationsTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,28 @@ public function testEmbedsManyEagerLoading()
609609
$this->assertEquals(2, $relations['addresses']->count());
610610
}
611611

612+
public function testEmbedsManyDelete()
613+
{
614+
$user1 = User::create(array('name' => 'John Doe'));
615+
$user1->addresses()->save(new Address(array('city' => 'New York')));
616+
$user1->addresses()->save(new Address(array('city' => 'Paris')));
617+
618+
$user2 = User::create(array('name' => 'Jane Doe'));
619+
$user2->addresses()->save(new Address(array('city' => 'Berlin')));
620+
$user2->addresses()->save(new Address(array('city' => 'Paris')));
621+
622+
$user1->addresses()->delete();
623+
$this->assertEquals(0, $user1->addresses()->count());
624+
$this->assertEquals(0, $user1->addresses->count());
625+
$this->assertEquals(2, $user2->addresses()->count());
626+
$this->assertEquals(2, $user2->addresses->count());
627+
628+
$user1 = User::find($user1->id);
629+
$user2 = User::find($user2->id);
630+
$this->assertEquals(0, $user1->addresses()->count());
631+
$this->assertEquals(0, $user1->addresses->count());
632+
$this->assertEquals(2, $user2->addresses()->count());
633+
$this->assertEquals(2, $user2->addresses->count());
634+
}
635+
612636
}

0 commit comments

Comments
 (0)