feat(blog): refactor author data retrieval to handle potential missing authors

This commit is contained in:
hamed
2026-06-19 00:35:07 +03:30
parent 1e45b12aec
commit 0f3bf68fdd
2 changed files with 19 additions and 5 deletions
+18 -4
View File
@@ -110,15 +110,29 @@ class Blog
'image_url' => $this->imageUrl,
'tags' => $this->tags,
'status' => $this->status,
'author' => [
'uuid' => $this->author->getUuid(),
'name' => $this->author->getRealName(),
],
'author' => $this->authorToArray(),
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
];
}
private function authorToArray(): ?array
{
if ($this->author === null) {
return null;
}
// The referenced user may have been deleted; a lazy proxy then throws
// EntityNotFoundException on access. Treat a missing author as null.
try {
return [
'uuid' => $this->author->getUuid(),
'name' => $this->author->getRealName(),
];
} catch (\Doctrine\ORM\EntityNotFoundException) {
return null;
}
}
public function toListArray(): array
{
return [