Skip to content

Commit b5efc30

Browse files
committed
Merge pull request #145 from neoxia/master-empty-embedded-mongoid
MongoId creation of embedded models edge case
2 parents 8ea8b96 + 120f936 commit b5efc30

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function get()
141141
public function save(Model $model)
142142
{
143143
// Insert a new document.
144-
if (!$model->exists)
144+
if ( ! $model->exists)
145145
{
146146
return $this->performInsert($model);
147147
}
@@ -162,7 +162,7 @@ public function save(Model $model)
162162
protected function performInsert(Model $model)
163163
{
164164
// Create a new key.
165-
if (!isset($model['_id']))
165+
if ( ! isset($model['_id']) or empty($model['_id']))
166166
{
167167
$model->setAttribute('_id', new MongoId);
168168
}

tests/RelationsTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,18 @@ public function testEmbedsManySaveMany()
341341

342342
public function testEmbedsManyCreate()
343343
{
344-
$user = User::create(array('name' => 'John Doe'));
345-
$user->addresses()->create(array('city' => 'Bruxelles'));
344+
$user = User::create(array());
345+
$address = $user->addresses()->create(array('city' => 'Bruxelles'));
346+
$this->assertInstanceOf('Address', $address);
347+
$this->assertInstanceOf('MongoID', $address->_id);
346348
$this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
347349

348350
$freshUser = User::find($user->id);
349351
$this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city'));
352+
353+
$user = User::create(array());
354+
$address = $user->addresses()->create(array('_id' => '', 'city' => 'Bruxelles'));
355+
$this->assertInstanceOf('MongoID', $address->_id);
350356
}
351357

352358
public function testEmbedsManyDestroy()

0 commit comments

Comments
 (0)