src/Entity/City.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CityRepository;
  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. /**
  9. * @Gedmo\Tree(type="nested")
  10. * @Serializer\ExclusionPolicy("ALL")
  11. */
  12. #[ORM\Entity(repositoryClass: CityRepository::class)]
  13. #[ORM\Table(name: '`city`')]
  14. class City
  15. {
  16. /**
  17. * @Serializer\Expose()
  18. */
  19. #[ORM\Id]
  20. #[ORM\GeneratedValue(strategy:"AUTO")]
  21. #[ORM\Column(type: "integer")]
  22. protected ?int $id = null;
  23. /**
  24. * @Serializer\Expose()
  25. */
  26. #[ORM\Column(type: "string", length: 200)]
  27. protected $name;
  28. #[ORM\Column(type: "string", nullable: true)]
  29. protected $number;
  30. /**
  31. * @Gedmo\TreeParent
  32. * @Serializer\Expose()
  33. */
  34. #[ORM\ManyToOne(targetEntity:"City", inversedBy: "children" ,fetch:"EAGER")]
  35. #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id", onDelete: "CASCADE")]
  36. protected $parent;
  37. #[ORM\OneToMany(targetEntity:"City", mappedBy:"parent", cascade: ['persist', 'remove'])]
  38. #[ORM\OrderBy(["lft" => "ASC"])]
  39. protected $children;
  40. /**
  41. * @Gedmo\TreeLeft
  42. */
  43. #[ORM\Column(type: "integer")]
  44. protected $lft;
  45. /**
  46. * @Gedmo\TreeRight
  47. */
  48. #[ORM\Column(type: "integer")]
  49. protected $rgt;
  50. /**
  51. * @Gedmo\TreeLevel
  52. */
  53. #[ORM\Column(type: "integer")]
  54. protected $lvl;
  55. /**
  56. * @Gedmo\TreeRoot
  57. */
  58. #[ORM\Column(type: "integer", nullable: true)]
  59. protected $root;
  60. public function __construct()
  61. {
  62. $this->children = new ArrayCollection;
  63. }
  64. public function getId()
  65. {
  66. return $this->id;
  67. }
  68. public function setName($name)
  69. {
  70. $this->name = $name;
  71. }
  72. public function getName()
  73. {
  74. return $this->name;
  75. }
  76. public function getNumber()
  77. {
  78. return $this->number;
  79. }
  80. public function setNumber($number)
  81. {
  82. $this->number = $number;
  83. }
  84. public function setParent(City $parent = null)
  85. {
  86. $this->parent = $parent;
  87. }
  88. public function getParent()
  89. {
  90. return $this->parent;
  91. }
  92. public function addChild(City $child)
  93. {
  94. $this->children[] = $child;
  95. $child->setParent($this);
  96. }
  97. public function removeChild(City $child)
  98. {
  99. $child->setParent(null);
  100. return $this->children->removeElement($child);
  101. }
  102. public function getChildren()
  103. {
  104. return $this->children;
  105. }
  106. public function __toString()
  107. {
  108. return $this->name;
  109. }
  110. public function setLft($lft)
  111. {
  112. $this->lft = $lft;
  113. }
  114. public function getLft()
  115. {
  116. return $this->lft;
  117. }
  118. public function setRgt($rgt)
  119. {
  120. $this->rgt = $rgt;
  121. }
  122. public function getRgt()
  123. {
  124. return $this->rgt;
  125. }
  126. public function setLvl($lvl)
  127. {
  128. $this->lvl = $lvl;
  129. }
  130. public function getLvl()
  131. {
  132. return $this->lvl;
  133. }
  134. public function setRoot($root)
  135. {
  136. $this->root = $root;
  137. }
  138. public function getRoot()
  139. {
  140. return $this->root;
  141. }
  142. public function isChildOf(City $category)
  143. {
  144. $me = $this;
  145. while ($me->parent) {
  146. if ($category === $me->parent) {
  147. return true;
  148. }
  149. $me = $me->parent;
  150. }
  151. return false;
  152. }
  153. }