From 0bfdfa81427e01ddedc89ac85a32f61b180970c1 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo Date: Wed, 27 Dec 2023 14:54:57 +0700 Subject: [PATCH] Use lazy-loading in `FileTrait`; Add informative exception if the file is missing in `FileTrait`. --- CHANGELOG.md | 5 +++++ packages/file-association-entity/src/FileTrait.php | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63bd7fb..df4f1eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 1.7.3 + +* Use lazy-loading in `FileTrait`. +* Add informative exception if the file is missing in `FileTrait`. + ## 1.7.2 * Fix typehints diff --git a/packages/file-association-entity/src/FileTrait.php b/packages/file-association-entity/src/FileTrait.php index 3f93663..acff5bd 100644 --- a/packages/file-association-entity/src/FileTrait.php +++ b/packages/file-association-entity/src/FileTrait.php @@ -31,7 +31,7 @@ trait FileTrait { use FileDecoratorTrait; - #[AsFileAssociation] + #[AsFileAssociation(fetch: 'LAZY')] private FileInterface $file; #[Embedded()] @@ -50,6 +50,10 @@ private function setWrapped(FileInterface $file): void private function getWrapped(): FileInterface { + if (!isset($this->file)) { + throw new \LogicException(sprintf('$file is not set. This might be caused by the use of `AbstractQuery::toIterable()`. If that is the case, you can: 1. stop involving "%s" in the query; 2. pre-hydrate the file entities before the query; or 3. use other means to iterate the query.', static::class)); + } + return FileDecorator::getFile($this->file, $this->getMetadata()) ?? new NullFile(); } }