src/Entity/Category.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use DateTimeInterface;
  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. use Symfony\Component\Validator\Constraints as Assert;
  10. /**
  11. * @Gedmo\Tree(type="nested")
  12. * @ORM\Entity(repositoryClass=CategoryRepository::class)
  13. */
  14. class Category
  15. {
  16. /**
  17. * @ORM\Id
  18. * @ORM\GeneratedValue
  19. * @ORM\Column(type="integer")
  20. */
  21. private $id;
  22. /**
  23. * @ORM\Column(type="string", length=255)
  24. * @Assert\Length(
  25. * min=4,
  26. * max=50,
  27. * maxMessage="Describe your category in 20 chars or less"
  28. * )
  29. * @Assert\NotBlank()
  30. */
  31. private $name;
  32. /**
  33. * @Gedmo\TreeLeft
  34. * @ORM\Column(type="integer")
  35. */
  36. private $lft;
  37. /**
  38. * @Gedmo\TreeRight
  39. * @ORM\Column(type="integer")
  40. */
  41. private $rgt;
  42. /**
  43. * @Gedmo\TreeParent
  44. * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  45. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  46. */
  47. private $parent;
  48. /**
  49. * @Gedmo\TreeRoot
  50. * @ORM\Column(type="integer", nullable=true)
  51. */
  52. private $root;
  53. /**
  54. * @Gedmo\TreeLevel
  55. * @ORM\Column(name="lvl", type="integer")
  56. */
  57. private $level;
  58. /**
  59. * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
  60. */
  61. private $children;
  62. /**
  63. * @Gedmo\Timestampable(on="create")
  64. * @ORM\Column(type="datetime")
  65. */
  66. private $created_at;
  67. /**
  68. * @Gedmo\Timestampable(on="update")
  69. * @ORM\Column(type="datetime")
  70. */
  71. private $updated_at;
  72. /**
  73. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="categories")
  74. * @ORM\JoinColumn(nullable=false)
  75. */
  76. private $created_by;
  77. /**
  78. * @ORM\ManyToMany(targetEntity=Post::class, mappedBy="categories")
  79. */
  80. private $posts;
  81. /**
  82. * @ORM\OneToMany(targetEntity=Product::class, mappedBy="category")
  83. */
  84. private $products = [];
  85. /**
  86. * @ORM\Column(type="json")
  87. */
  88. private $content = [];
  89. /**
  90. * @Gedmo\Slug(fields={"name"})
  91. * @ORM\Column(type="string", length=255)
  92. */
  93. private $slug;
  94. public function __construct()
  95. {
  96. $this->posts = new ArrayCollection();
  97. $this->products = new ArrayCollection();
  98. }
  99. public function getId(): ?int
  100. {
  101. return $this->id;
  102. }
  103. public function getName(): ?string
  104. {
  105. return $this->name;
  106. }
  107. public function setName(string $name): self
  108. {
  109. $this->name = $name;
  110. return $this;
  111. }
  112. public function getCreatedAt(): ?DateTimeInterface
  113. {
  114. return $this->created_at;
  115. }
  116. public function getUpdatedAt(): ?DateTimeInterface
  117. {
  118. return $this->updated_at;
  119. }
  120. public function setParent($parent)
  121. {
  122. $this->parent = $parent;
  123. return $this;
  124. }
  125. public function getParent()
  126. {
  127. return $this->parent;
  128. }
  129. public function getRoot()
  130. {
  131. return $this->root;
  132. }
  133. public function getLevel()
  134. {
  135. return $this->level;
  136. }
  137. public function getChildren()
  138. {
  139. return $this->children;
  140. }
  141. public function getLeft()
  142. {
  143. return $this->lft;
  144. }
  145. public function getRight()
  146. {
  147. return $this->rgt;
  148. }
  149. public function getCreatedBy(): ?User
  150. {
  151. return $this->created_by;
  152. }
  153. public function setCreatedBy(?User $owner): self
  154. {
  155. $this->created_by = $owner;
  156. return $this;
  157. }
  158. /**
  159. * @return Collection|Post[]
  160. */
  161. public function getPosts(): Collection
  162. {
  163. return $this->posts;
  164. }
  165. public function addPost(Post $post): self
  166. {
  167. if (!$this->posts->contains($post)) {
  168. $this->posts[] = $post;
  169. $post->addCategory($this);
  170. }
  171. return $this;
  172. }
  173. public function removePost(Post $post): self
  174. {
  175. if ($this->posts->removeElement($post)) {
  176. $post->removeCategory($this);
  177. }
  178. return $this;
  179. }
  180. public function __toString()
  181. {
  182. return $this->getName() ?? '';
  183. }
  184. /**
  185. * @return Collection|Product[]
  186. */
  187. public function getProducts(): Collection
  188. {
  189. return $this->products;
  190. }
  191. public function addProduct(Product $product): self
  192. {
  193. if (!$this->products->contains($product)) {
  194. $this->products[] = $product;
  195. $product->setCategory($this);
  196. }
  197. return $this;
  198. }
  199. public function removeProduct(Product $product): self
  200. {
  201. if ($this->products->removeElement($product)) {
  202. // set the owning side to null (unless already changed)
  203. if ($product->getCategory() === $this) {
  204. $product->setCategory(null);
  205. }
  206. }
  207. return $this;
  208. }
  209. public function getContent(): ?array
  210. {
  211. return $this->content;
  212. }
  213. public function setContent(array $content): self
  214. {
  215. $this->content = $content;
  216. return $this;
  217. }
  218. public function getSlug(): ?string
  219. {
  220. return $this->slug;
  221. }
  222. }