'ASC'))] #[ManyToMany(Category::class, 'articles', cascade: CascadeType::PERSIST)] public \ArrayObject $categories; #[OneToMany(Comment::class, 'article', cascade: CascadeType::PERSIST, orphanRemoval: true)] public \ArrayObject $comments; function __construct() { $this->categories = new \ArrayObject(); $this->comments = new \ArrayObject(); } private function getCategoryIds() { $categories = array(); foreach($this->categories as $category) { $categories[] = $category->basicJsonSerialize(); } return $categories; } private function getCommentIds() { $comments = array(); foreach($this->comments as $comment) { $comments[] = $comment->basicJsonSerialize(); } return $comments; } function basicJsonSerialize() { return [ 'id' => $this->id, 'title' => $this->title, 'text' => $this->text ]; } function jsonSerialize(): mixed { $basicJson = $this->basicJsonSerialize(); $basicJson['categoryName'] = $this->categoryName; $basicJson['categories'] = $this->getCategoryIds(); $basicJson['comments'] = $this->getCommentIds(); return $basicJson; } }