feat(blog): refactor author data retrieval to handle potential missing authors
This commit is contained in:
@@ -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 [
|
||||
|
||||
Reference in New Issue
Block a user