Skip to content

Commit

Permalink
[BUGFIX] Improve type safety for lazy-loaded associations (#1592)
Browse files Browse the repository at this point in the history
`_loadRealInstance` will return `null` if the target of the
association does not exist (anymore). We need to handle that case
in a clean way.

Also use a dedicated folder for the fixture to make the fixture
easier to find, and to discourage reuse of fixtures.
  • Loading branch information
oliverklee authored Feb 4, 2025
1 parent dfc4a1b commit fb5066a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Classes/Domain/Model/Tea.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public function setDescription(string $description): void
public function getImage(): ?FileReference
{
if ($this->image instanceof LazyLoadingProxy) {
/** @var FileReference $image */
$image = $this->image->_loadRealInstance();
$this->image = $image;
$this->image = ($image instanceof FileReference) ? $image : null;
}

return $this->image;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"tx_tea_domain_model_tea"
,"uid","title","image",
,1,"Gunpowder",1
13 changes: 13 additions & 0 deletions Tests/Functional/Domain/Repository/TeaRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ public function fillsImageRelation(): void
self::assertSame(1, $image->getUid());
}

/**
* @test
*/
public function MapsDeletedImageRelationToNull(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/propertyMapping/TeaWithDeletedImage.csv');

$model = $this->subject->findByUid(1);
self::assertInstanceOf(Tea::class, $model);

self::assertNull($model->getImage());
}

/**
* @test
*/
Expand Down

0 comments on commit fb5066a

Please sign in to comment.