Guest User

Untitled

a guest
Sep 1st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
  11.  
  12. #[Gedmo\Tree(type: 'nested')]
  13. #[ORM\Entity(repositoryClass: NestedTreeRepository::class)]
  14. class Category
  15. {
  16. #[ORM\Id]
  17. #[ORM\GeneratedValue]
  18. #[ORM\Column(type: Types::INTEGER)]
  19. private $id;
  20.  
  21. #[ORM\Column(name: 'name', type: Types::STRING, length: 80)]
  22. private $name;
  23.  
  24. #[Gedmo\TreeLeft]
  25. #[ORM\Column(name: 'lft', type: Types::INTEGER)]
  26. private $lft;
  27.  
  28. #[Gedmo\TreeLevel]
  29. #[ORM\Column(name: 'lvl', type: Types::INTEGER)]
  30. private $lvl;
  31.  
  32. #[Gedmo\TreeRight]
  33. #[ORM\Column(name: 'rgt', type: Types::INTEGER)]
  34. private $rgt;
  35.  
  36. #[Gedmo\TreeRoot]
  37. #[ORM\ManyToOne(targetEntity: Category::class)]
  38. #[ORM\JoinColumn(name: 'tree_root', referencedColumnName: 'id', onDelete: 'CASCADE')]
  39. private $root;
  40.  
  41. #[Gedmo\TreeParent]
  42. #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'children')]
  43. #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
  44. private $parent;
  45.  
  46. #[ORM\OneToMany(targetEntity: Category::class, mappedBy: 'parent')]
  47. #[ORM\OrderBy(['lft' => 'ASC'])]
  48. private $children;
  49.  
  50. #[ORM\OneToMany(mappedBy: 'category', targetEntity: Product::class)]
  51. private Collection $products;
  52.  
  53. #[ORM\ManyToMany(targetEntity: Property::class, mappedBy: 'category')]
  54. private Collection $properties;
  55.  
  56. public function __construct()
  57. {
  58. $this->products = new ArrayCollection();
  59. $this->properties = new ArrayCollection();
  60. }
  61.  
  62. public function getId(): ?int
  63. {
  64. return $this->id;
  65. }
  66.  
  67. public function setName(?string $name): void
  68. {
  69. $this->name = $name;
  70. }
  71.  
  72. public function getName(): ?string
  73. {
  74. return $this->name;
  75. }
  76.  
  77. public function getRoot(): ?self
  78. {
  79. return $this->root;
  80. }
  81.  
  82. public function setParent(self $parent = null): void
  83. {
  84. $this->parent = $parent;
  85. }
  86.  
  87. public function getParent(): ?self
  88. {
  89. return $this->parent;
  90. }
  91.  
  92. /**
  93. * @return Collection<int, Product>
  94. */
  95. public function getProducts(): Collection
  96. {
  97. return $this->products;
  98. }
  99.  
  100. public function addProduct(Product $product): self
  101. {
  102. if (!$this->products->contains($product)) {
  103. $this->products->add($product);
  104. $product->setCategory($this);
  105. }
  106.  
  107. return $this;
  108. }
  109.  
  110. public function removeProduct(Product $product): self
  111. {
  112. if ($this->products->removeElement($product)) {
  113. // set the owning side to null (unless already changed)
  114. if ($product->getCategory() === $this) {
  115. $product->setCategory(null);
  116. }
  117. }
  118.  
  119. return $this;
  120. }
  121.  
  122. /**
  123. * @return Collection<int, Property>
  124. */
  125. public function getProperties(): Collection
  126. {
  127. return $this->properties;
  128. }
  129.  
  130. public function addProperty(Property $property): self
  131. {
  132. if (!$this->properties->contains($property)) {
  133. $this->properties->add($property);
  134. $property->addCategory($this);
  135. }
  136.  
  137. return $this;
  138. }
  139.  
  140. public function removeProperty(Property $property): self
  141. {
  142. if ($this->properties->removeElement($property)) {
  143. $property->removeCategory($this);
  144. }
  145.  
  146. return $this;
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment