| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace internship\bo;
- use n2n\reflection\ObjectAdapter;
- use n2n\persistence\orm\attribute\ManyToMany;
- use n2n\persistence\orm\attribute\OneToMany;
- use n2n\persistence\orm\CascadeType;
- use n2n\persistence\orm\attribute\OrderBy;
- class Article extends ObjectAdapter implements \JsonSerializable {
- public int $id;
- public string $categoryName;
- public string $text;
- public string $title;
- #[OrderBy(array('name' => '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;
- }
- }
|