Commit b5efc304 authored by Jens Segers's avatar Jens Segers

Merge pull request #145 from neoxia/master-empty-embedded-mongoid

MongoId creation of embedded models edge case
parents 8ea8b96f 120f936b
...@@ -141,7 +141,7 @@ class EmbedsMany extends Relation { ...@@ -141,7 +141,7 @@ class EmbedsMany extends Relation {
public function save(Model $model) public function save(Model $model)
{ {
// Insert a new document. // Insert a new document.
if (!$model->exists) if ( ! $model->exists)
{ {
return $this->performInsert($model); return $this->performInsert($model);
} }
...@@ -162,7 +162,7 @@ class EmbedsMany extends Relation { ...@@ -162,7 +162,7 @@ class EmbedsMany extends Relation {
protected function performInsert(Model $model) protected function performInsert(Model $model)
{ {
// Create a new key. // Create a new key.
if (!isset($model['_id'])) if ( ! isset($model['_id']) or empty($model['_id']))
{ {
$model->setAttribute('_id', new MongoId); $model->setAttribute('_id', new MongoId);
} }
......
...@@ -341,12 +341,18 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -341,12 +341,18 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
public function testEmbedsManyCreate() public function testEmbedsManyCreate()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array());
$user->addresses()->create(array('city' => 'Bruxelles')); $address = $user->addresses()->create(array('city' => 'Bruxelles'));
$this->assertInstanceOf('Address', $address);
$this->assertInstanceOf('MongoID', $address->_id);
$this->assertEquals(array('Bruxelles'), $user->addresses->lists('city')); $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
$freshUser = User::find($user->id); $freshUser = User::find($user->id);
$this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city')); $this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city'));
$user = User::create(array());
$address = $user->addresses()->create(array('_id' => '', 'city' => 'Bruxelles'));
$this->assertInstanceOf('MongoID', $address->_id);
} }
public function testEmbedsManyDestroy() public function testEmbedsManyDestroy()
......
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