Skip to content

Commit 40f7264

Browse files
authored
Merge branch 'master' into master
2 parents a1777c9 + a7acac7 commit 40f7264

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
github: jenssegers
2-
open_collective: laravel-mongodb
2+
tidelift: "packagist/jenssegers/mongodb"

LICENSE renamed to LICENSE.md

File renamed without changes.

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1134,3 +1134,7 @@ Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rathe
11341134
```php
11351135
$books = $user->books()->sortBy('title')->get();
11361136
```
1137+
1138+
## Security contact information
1139+
1140+
To report a security vulnerability, follow [these steps](https://tidelift.com/security).

src/Jenssegers/Mongodb/Relations/EmbedsOne.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
class EmbedsOne extends EmbedsOneOrMany
1010
{
11-
/**
12-
* @inheritdoc
13-
*/
1411
public function initRelation(array $models, $relation)
1512
{
1613
foreach ($models as $model) {
@@ -20,14 +17,19 @@ public function initRelation(array $models, $relation)
2017
return $models;
2118
}
2219

23-
/**
24-
* @inheritdoc
25-
*/
2620
public function getResults()
2721
{
2822
return $this->toModel($this->getEmbedded());
2923
}
3024

25+
public function getEager()
26+
{
27+
$eager = $this->get();
28+
29+
// EmbedsOne only brings one result, Eager needs a collection!
30+
return $this->toCollection([$eager]);
31+
}
32+
3133
/**
3234
* Save a new model and attach it to the parent model.
3335
* @param Model $model

tests/EmbeddedRelationsTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,36 @@ public function testEmbedsOneDelete()
614614
$this->assertNull($user->father);
615615
}
616616

617+
public function testEmbedsOneRefresh()
618+
{
619+
$user = User::create(['name' => 'John Doe']);
620+
$father = new User(['name' => 'Mark Doe']);
621+
622+
$user->father()->associate($father);
623+
$user->save();
624+
625+
$user->refresh();
626+
627+
$this->assertNotNull($user->father);
628+
$this->assertEquals('Mark Doe', $user->father->name);
629+
}
630+
631+
public function testEmbedsOneEmptyRefresh()
632+
{
633+
$user = User::create(['name' => 'John Doe']);
634+
$father = new User(['name' => 'Mark Doe']);
635+
636+
$user->father()->associate($father);
637+
$user->save();
638+
639+
$user->father()->dissociate();
640+
$user->save();
641+
642+
$user->refresh();
643+
644+
$this->assertNull($user->father);
645+
}
646+
617647
public function testEmbedsManyToArray()
618648
{
619649
/** @var User $user */
@@ -627,6 +657,22 @@ public function testEmbedsManyToArray()
627657
$this->assertIsArray($array['addresses']);
628658
}
629659

660+
public function testEmbedsManyRefresh()
661+
{
662+
/** @var User $user */
663+
$user = User::create(['name' => 'John Doe']);
664+
$user->addresses()->save(new Address(['city' => 'New York']));
665+
$user->addresses()->save(new Address(['city' => 'Paris']));
666+
$user->addresses()->save(new Address(['city' => 'Brussels']));
667+
668+
$user->refresh();
669+
670+
$array = $user->toArray();
671+
672+
$this->assertArrayHasKey('addresses', $array);
673+
$this->assertIsArray($array['addresses']);
674+
}
675+
630676
public function testEmbeddedSave()
631677
{
632678
/** @var User $user */

0 commit comments

Comments
 (0)