src/Entity/Post.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\SuperClass\BaseEntity;
  4. use App\Repository\PostRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. /**
  10. * @ORM\Entity(repositoryClass=PostRepository::class)
  11. */
  12. class Post extends BaseEntity
  13. {
  14. /**
  15. * @ORM\Column(type="string", length=255)
  16. */
  17. private $title;
  18. /**
  19. * @ORM\Column(type="json")
  20. */
  21. private $content = [];
  22. /**
  23. * @Gedmo\Slug(fields={"title"})
  24. * @ORM\Column(type="string", length=255)
  25. */
  26. private $slug;
  27. /**
  28. * @ORM\Column(type="text")
  29. */
  30. private $metadata;
  31. /**
  32. * @ORM\Column(type="array")
  33. */
  34. private $tags = [];
  35. /**
  36. * @ORM\ManyToMany(targetEntity=Category::class, inversedBy="posts")
  37. */
  38. private $categories;
  39. /**
  40. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="posts")
  41. * @ORM\JoinColumn(nullable=false)
  42. */
  43. private $created_by;
  44. /**
  45. * @ORM\Column(type="datetime", nullable="true")
  46. */
  47. private $published_at;
  48. /**
  49. * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="post")
  50. */
  51. private $comments;
  52. public function __construct()
  53. {
  54. $this->categories = new ArrayCollection();
  55. $this->comments = new ArrayCollection();
  56. }
  57. public function getId(): ?int
  58. {
  59. return $this->id;
  60. }
  61. public function getTitle(): ?string
  62. {
  63. return $this->title;
  64. }
  65. public function setTitle(string $title): self
  66. {
  67. $this->title = $title;
  68. return $this;
  69. }
  70. public function getContent(): ?array
  71. {
  72. return $this->content;
  73. }
  74. public function setContent(array $content): self
  75. {
  76. $this->content = $content;
  77. return $this;
  78. }
  79. public function getSlug(): ?string
  80. {
  81. return $this->slug;
  82. }
  83. public function getMetadata(): ?string
  84. {
  85. return $this->metadata;
  86. }
  87. public function setMetadata(string $metadata): self
  88. {
  89. $this->metadata = $metadata;
  90. return $this;
  91. }
  92. public function getTags(): ?array
  93. {
  94. return $this->tags;
  95. }
  96. public function setTags(array $tags): self
  97. {
  98. $this->tags = $tags;
  99. return $this;
  100. }
  101. /**
  102. * @return Collection|Category[]
  103. */
  104. public function getCategories(): Collection
  105. {
  106. return $this->categories;
  107. }
  108. public function addCategory(Category $category): self
  109. {
  110. if (!$this->categories->contains($category)) {
  111. $this->categories[] = $category;
  112. }
  113. return $this;
  114. }
  115. public function removeCategory(Category $category): self
  116. {
  117. $this->categories->removeElement($category);
  118. return $this;
  119. }
  120. public function getCreatedBy(): ?User
  121. {
  122. return $this->created_by;
  123. }
  124. public function setCreatedBy(?User $created_by): self
  125. {
  126. $this->created_by = $created_by;
  127. return $this;
  128. }
  129. public function getPublishedAt(): ?\DateTimeInterface
  130. {
  131. return $this->published_at;
  132. }
  133. public function setPublishedAt(?\DateTimeInterface $published_at): self
  134. {
  135. $this->published_at = $published_at;
  136. return $this;
  137. }
  138. /**
  139. * @return Collection|Comment[]
  140. */
  141. public function getComments(): Collection
  142. {
  143. return $this->comments;
  144. }
  145. public function addComment(Comment $comment): self
  146. {
  147. if (!$this->comments->contains($comment)) {
  148. $this->comments[] = $comment;
  149. $comment->setPost($this);
  150. }
  151. return $this;
  152. }
  153. public function removeComment(Comment $comment): self
  154. {
  155. if ($this->comments->removeElement($comment)) {
  156. // set the owning side to null (unless already changed)
  157. if ($comment->getPost() === $this) {
  158. $comment->setPost(null);
  159. }
  160. }
  161. return $this;
  162. }
  163. }