Commit 31f54d2b authored by Alexandre Butynski's avatar Alexandre Butynski

Fix #175 : embedsMany duplicates are identified by key

parent 9ca3f159
...@@ -166,7 +166,7 @@ class EmbedsMany extends Relation { ...@@ -166,7 +166,7 @@ class EmbedsMany extends Relation {
$this->updateTimestamps($model); $this->updateTimestamps($model);
// Insert a new document. // Insert a new document.
if ( ! $model->exists) if ( ! $this->contains($model))
{ {
$result = $this->performInsert($model); $result = $this->performInsert($model);
} }
...@@ -196,6 +196,22 @@ class EmbedsMany extends Relation { ...@@ -196,6 +196,22 @@ class EmbedsMany extends Relation {
return count($this->getEmbeddedRecords()); return count($this->getEmbeddedRecords());
} }
/**
* Indicate if a model is already contained in the embedded documents
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
public function contains(Model $model)
{
foreach ($this->getEmbeddedRecords() as $record)
{
if ($record[$model->getKeyName()] == $model->getKey()) return true;
}
return false;
}
/** /**
* Attach a model instance to the parent model without persistence. * Attach a model instance to the parent model without persistence.
* *
...@@ -205,7 +221,7 @@ class EmbedsMany extends Relation { ...@@ -205,7 +221,7 @@ class EmbedsMany extends Relation {
public function associate(Model $model) public function associate(Model $model)
{ {
// Insert the related model in the parent instance // Insert the related model in the parent instance
if ( ! $model->exists) if ( ! $this->contains($model))
{ {
return $this->associateNew($model); return $this->associateNew($model);
} }
......
...@@ -399,6 +399,10 @@ class RelationsTest extends TestCase { ...@@ -399,6 +399,10 @@ class RelationsTest extends TestCase {
$user->addresses()->save($address); $user->addresses()->save($address);
$this->assertEquals(1, $user->addresses->count()); $this->assertEquals(1, $user->addresses->count());
$this->assertEquals(array('Paris'), $user->addresses->lists('city')); $this->assertEquals(array('Paris'), $user->addresses->lists('city'));
$user->addresses()->create(array('_id' => $address->_id, 'city' => 'Bruxelles'));
$this->assertEquals(1, $user->addresses->count());
$this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
} }
public function testEmbedsManyCreate() public function testEmbedsManyCreate()
...@@ -521,7 +525,7 @@ class RelationsTest extends TestCase { ...@@ -521,7 +525,7 @@ class RelationsTest extends TestCase {
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
$address = new Address(array('city' => 'New York')); $address = new Address(array('city' => 'New York'));
$address->exists = true; $user->addresses()->save($address);
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment