Skip to content

Commit e914ba2

Browse files
committed
Merge branch 'develop'
2 parents e8508cb + e82e047 commit e914ba2

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/LeanMapper/Entity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,12 @@ protected function initDefaults(): void
858858
private function getHasOneValue(Property $property, Relationship\HasOne $relationship, ?Filtering $filtering = null): ?Entity
859859
{
860860
if (!$relationship->hasTargetTable()) {
861+
$viaColumn = $relationship->getColumnReferencingTargetTable();
862+
863+
if ($this->row->hasReferencedRow($viaColumn) && $this->row->getReferencedRow($viaColumn) === null && $property->isNullable()) {
864+
return null;
865+
}
866+
861867
throw new InvalidStateException('Cannot get referenced Entity for detached Entity.');
862868
}
863869
$targetTable = $relationship->getTargetTable();

src/LeanMapper/Row.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace LeanMapper;
1515

16+
use LeanMapper\Exception\InvalidStateException;
17+
1618
/**
1719
* Pointer to specific position within Result instance
1820
*
@@ -225,6 +227,22 @@ public function setReferencedRow(?self $row = null, string $viaColumn): void
225227
}
226228

227229

230+
public function hasReferencedRow(string $viaColumn): bool
231+
{
232+
return array_key_exists($viaColumn, $this->referencedRows);
233+
}
234+
235+
236+
public function getReferencedRow(string $viaColumn): ?Row
237+
{
238+
if (array_key_exists($viaColumn, $this->referencedRows)) {
239+
return $this->referencedRows[$viaColumn];
240+
}
241+
242+
throw new InvalidStateException("Missing referenced row for column '$viaColumn'.");
243+
}
244+
245+
228246
/**
229247
* Adds new data entry to referencing Result
230248
*

tests/LeanMapper/Entity.hasOne.detached.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Author extends Entity
2121
* @property int $id
2222
* @property string $name
2323
* @property Author $author m:hasOne
24+
* @property Author|null $reviewer m:hasOne
2425
*/
2526
class Book extends Entity
2627
{
@@ -58,3 +59,24 @@ $authorRepository->persist($author);
5859
$book = new Book;
5960
$book->author = $author;
6061
Assert::same('Fowler', $book->author->name);
62+
63+
////////////////////
64+
65+
test('Assign NULL to detached', function () {
66+
$book = new Book;
67+
$book->reviewer = null;
68+
69+
Assert::null($book->reviewer);
70+
});
71+
72+
73+
test('Assign attached & NULL to detached', function () use ($authorRepository) {
74+
$author = $authorRepository->find(1);
75+
76+
$book = new Book;
77+
$book->author = $author;
78+
$book->reviewer = null;
79+
80+
Assert::same('Andrew Hunt', $book->author->name);
81+
Assert::null($book->reviewer);
82+
});

tests/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@
3636
$mapper = new DefaultMapper(null);
3737

3838
$entityFactory = new DefaultEntityFactory;
39+
40+
41+
function test(string $title, Closure $function): void
42+
{
43+
$function();
44+
}

0 commit comments

Comments
 (0)