src/Entity/Category.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use JMS\Serializer\Annotation as Serializer;
  8. #[Gedmo\Tree(type: 'nested')]
  9. #[Serializer\ExclusionPolicy("ALL")]
  10. #[ORM\Entity(repositoryClass: CategoryRepository::class)]
  11. #[ORM\Table(name: '`category`')]
  12. class Category
  13. {
  14. #[ORM\Id]
  15. #[ORM\GeneratedValue(strategy:"AUTO")]
  16. #[ORM\Column(type: "integer")]
  17. #[Serializer\Expose]
  18. protected ?int $id = null;
  19. #[ORM\Column(type: "boolean")]
  20. private $enabled = true;
  21. /**
  22. * @Serializer\Expose()
  23. */
  24. #[ORM\Column(type: "string", length: 250)]
  25. protected $name;
  26. /**
  27. * @Gedmo\Slug(handlers={
  28. * @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
  29. * @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
  30. * @Gedmo\SlugHandlerOption(name="separator", value="/")
  31. * })
  32. * }, fields={"name"})
  33. * @Serializer\Expose()
  34. */
  35. #[ORM\Column(type: "string", length: 128, unique: true, nullable: true )]
  36. protected $slug;
  37. #[Gedmo\TreeParent]
  38. #[Serializer\Expose]
  39. #[ORM\ManyToOne(targetEntity:"Category", inversedBy:"children", fetch: "EAGER")]
  40. #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id", onDelete: "CASCADE")]
  41. protected $parent;
  42. #[ORM\OneToMany(targetEntity:"Category", mappedBy:"parent", orphanRemoval: true, cascade: ['all'])]
  43. #[ORM\OrderBy(["lft" => "ASC"])]
  44. #[ORM\JoinColumn(onDelete: "CASCADE")]
  45. protected $children;
  46. #[Gedmo\TreeLeft]
  47. #[ORM\Column(type: "integer")]
  48. protected $lft;
  49. #[Gedmo\TreeRight]
  50. #[ORM\Column(type: "integer")]
  51. protected $rgt;
  52. #[Gedmo\TreeLevel]
  53. #[ORM\Column(type: "integer")]
  54. protected $lvl;
  55. #[Gedmo\TreeRoot]
  56. #[ORM\Column(type: "integer", nullable: true)]
  57. protected $root;
  58. public function __construct()
  59. {
  60. $this->children = new ArrayCollection;
  61. }
  62. public function getId()
  63. {
  64. return $this->id;
  65. }
  66. public function setName($name)
  67. {
  68. $this->name = $name;
  69. }
  70. public function getName()
  71. {
  72. return $this->name;
  73. }
  74. public function getSlug()
  75. {
  76. return $this->slug;
  77. }
  78. public function setSlug($slug)
  79. {
  80. $this->slug = $slug;
  81. }
  82. public function setParent($parent)
  83. {
  84. $this->parent = $parent;
  85. }
  86. public function getParent()
  87. {
  88. return $this->parent;
  89. }
  90. public function addChild(Category $child)
  91. {
  92. if (!$this->children->contains($child)) {
  93. $child->setParent($this);
  94. $this->children->add($child);
  95. }
  96. }
  97. public function removeChild(Category $child)
  98. {
  99. if ($this->children->contains($child)) {
  100. $child->setParent(null);
  101. $this->children->removeElement($child);
  102. }
  103. }
  104. public function getChildren()
  105. {
  106. return $this->children;
  107. }
  108. public function __toString()
  109. {
  110. return $this->name;
  111. }
  112. public function setLft($lft)
  113. {
  114. $this->lft = $lft;
  115. }
  116. public function getLft()
  117. {
  118. return $this->lft;
  119. }
  120. public function setRgt($rgt)
  121. {
  122. $this->rgt = $rgt;
  123. }
  124. public function getRgt()
  125. {
  126. return $this->rgt;
  127. }
  128. public function setLvl($lvl)
  129. {
  130. $this->lvl = $lvl;
  131. }
  132. public function getLvl()
  133. {
  134. return $this->lvl;
  135. }
  136. public function setRoot($root)
  137. {
  138. $this->root = $root;
  139. }
  140. public function getRoot()
  141. {
  142. return $this->root;
  143. }
  144. public function isChildOf(Category $category)
  145. {
  146. $me = $this;
  147. while ($me->parent) {
  148. if ($category === $me->parent) {
  149. return true;
  150. }
  151. $me = $me->parent;
  152. }
  153. return false;
  154. }
  155. public function getEnabled()
  156. {
  157. return $this->enabled;
  158. }
  159. public function setEnabled($enabled)
  160. {
  161. $this->enabled = $enabled;
  162. }
  163. public function getPath()
  164. {
  165. if ($this->lvl === 1 || $this->lvl === 0) {
  166. return $this->name;
  167. }
  168. return $this->parent->getName().'/'.$this->name;
  169. }
  170. }