Skip to content

Commit 3c20c17

Browse files
committed
Merge pull request #741 from Rezouce/2.3
EmbedsOne relationship returns null when no model is associated
2 parents 6e9b5f4 + 3bc5120 commit 3c20c17

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ protected function getEmbedded()
233233
// Get raw attributes to skip relations and accessors.
234234
$attributes = $this->parent->getAttributes();
235235

236-
$embedded = isset($attributes[$this->localKey]) ? (array) $attributes[$this->localKey] : [];
236+
$embedded = isset($attributes[$this->localKey]) ? (array) $attributes[$this->localKey] : null;
237237

238238
return $embedded;
239239
}

tests/EmbeddedRelationsTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,12 @@ public function testEmbedsOneAssociate()
505505
$this->assertEquals('Mark Doe', $user->father->name);
506506
}
507507

508+
public function testEmbedsOneNullAssociation()
509+
{
510+
$user = User::create();
511+
$this->assertNull($user->father);
512+
}
513+
508514
public function testEmbedsOneDelete()
509515
{
510516
$user = User::create(['name' => 'John Doe']);

0 commit comments

Comments
 (0)