Skip to content

Commit

Permalink
Adding delete method for embedded documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Apr 19, 2014
1 parent 418c3db commit b9e1efe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 24 deletions.
78 changes: 54 additions & 24 deletions src/Jenssegers/Mongodb/Relations/EmbedsMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ public function find($id)
}

$records = $this->getEmbeddedRecords();

$primaryKey = $this->related->getKeyName();

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

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

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

$records = $this->getEmbeddedRecords();
$primaryKey = $this->related->getKeyName();

// Get existing embedded documents.
$documents = $this->getEmbeddedRecords();

// Remove the document from the parent model.
foreach ($documents as $i => $document)
foreach ($records as $i => $record)
{
if (in_array($document[$primaryKey], $ids))
if (in_array($record[$primaryKey], $ids))
{
unset($documents[$i]);
unset($records[$i]);
}
}

$this->setEmbeddedRecords($documents);
$this->setEmbeddedRecords($records);

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

/**
* Delete alias.
* Delete all embedded models.
*
* @return int
*/
public function delete()
{
// Overwrite the local key with an empty array.
$result = $this->query->update(array($this->localKey => array()));

// If the update query was successful, we will remove the embedded records
// of the parent instance.
if ($result)
{
$count = $this->count();

$this->setEmbeddedRecords(array());

// Return the number of deleted embedded records.
return $count;
}

return $result;
}

/**
* Destroy alias.
*
* @param mixed $ids
* @return int
Expand Down Expand Up @@ -356,14 +381,20 @@ protected function performInsert(Model $model)
{
if ($this->fireModelEvent($model, 'creating') === false) return false;

// Associate the new model to the parent.
$this->associateNew($model);
// Create a new key if needed.
if ( ! $model->getAttribute('_id'))
{
$model->setAttribute('_id', new MongoId);
}

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

if ($result)
{
// Associate the new model to the parent.
$this->associateNew($model);

$this->fireModelEvent($model, 'created', false);

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

// Update the related model in the parent instance
$this->associateExisting($model);

// Get the correct foreign key value.
$id = $this->getForeignKeyValue($model);

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

if ($result)
{
// Update the related model in the parent instance
$this->associateExisting($model);

$this->fireModelEvent($model, 'updated', false);

return $model;
Expand Down Expand Up @@ -438,18 +469,18 @@ protected function performDelete(Model $model)
*/
protected function associateNew($model)
{
// Create a new key.
// Create a new key if needed.
if ( ! $model->getAttribute('_id'))
{
$model->setAttribute('_id', new MongoId);
}

$documents = $this->getEmbeddedRecords();
$records = $this->getEmbeddedRecords();

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

$this->setEmbeddedRecords($documents);
$this->setEmbeddedRecords($records);

// Mark the model as existing.
$model->exists = true;
Expand All @@ -466,23 +497,22 @@ protected function associateNew($model)
protected function associateExisting($model)
{
// Get existing embedded documents.
$documents = $this->getEmbeddedRecords();
$records = $this->getEmbeddedRecords();

$primaryKey = $this->related->getKeyName();

$key = $model->getKey();

// Replace the document in the parent model.
foreach ($documents as &$document)
foreach ($records as &$record)
{
if ($document[$primaryKey] == $key)
if ($record[$primaryKey] == $key)
{
$document = $model->getAttributes();
$record = $model->getAttributes();
break;
}
}

$this->setEmbeddedRecords($documents);
$this->setEmbeddedRecords($records);

return $model;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,28 @@ public function testEmbedsManyEagerLoading()
$this->assertEquals(2, $relations['addresses']->count());
}

public function testEmbedsManyDelete()
{
$user1 = User::create(array('name' => 'John Doe'));
$user1->addresses()->save(new Address(array('city' => 'New York')));
$user1->addresses()->save(new Address(array('city' => 'Paris')));

$user2 = User::create(array('name' => 'Jane Doe'));
$user2->addresses()->save(new Address(array('city' => 'Berlin')));
$user2->addresses()->save(new Address(array('city' => 'Paris')));

$user1->addresses()->delete();
$this->assertEquals(0, $user1->addresses()->count());
$this->assertEquals(0, $user1->addresses->count());
$this->assertEquals(2, $user2->addresses()->count());
$this->assertEquals(2, $user2->addresses->count());

$user1 = User::find($user1->id);
$user2 = User::find($user2->id);
$this->assertEquals(0, $user1->addresses()->count());
$this->assertEquals(0, $user1->addresses->count());
$this->assertEquals(2, $user2->addresses()->count());
$this->assertEquals(2, $user2->addresses->count());
}

}

0 comments on commit b9e1efe

Please sign in to comment.