Node.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace CablemodemBundle\Entity;
  3. use Base\AdminBundle\Interfaces\PreRemoveInterface;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JMS\Serializer\Annotation as JMS;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Base\AdminBundle\Traits\TenancyIdTrait;
  10. use Base\AdminBundle\Traits\TenancyIdTraitInterface;
  11. /**
  12. * @ORM\Entity
  13. * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name","tenancy_id"})})
  14. *
  15. * @UniqueEntity(fields={"tenancyId","name"})
  16. */
  17. class Node implements TenancyIdTraitInterface, PreRemoveInterface
  18. {
  19. use TenancyIdTrait;
  20. /**
  21. * @ORM\Column(name="id", type="bigint", nullable=false)
  22. * @ORM\Id
  23. * @ORM\GeneratedValue(strategy="IDENTITY")
  24. */
  25. protected $id;
  26. /**
  27. * @ORM\Column(type="string", length=100)
  28. *
  29. * @Assert\NotNull
  30. */
  31. protected $name;
  32. /**
  33. * @ORM\ManyToOne(targetEntity="Node", inversedBy="childs")
  34. * @ORM\JoinColumn(onDelete="CASCADE")
  35. *
  36. * @JMS\MaxDepth(1)
  37. */
  38. protected $parent;
  39. /**
  40. * @ORM\OneToMany(targetEntity="Node", mappedBy="parent")
  41. *
  42. * @JMS\MaxDepth(1)
  43. */
  44. protected $childs;
  45. /**
  46. * Constructor
  47. */
  48. public function __construct()
  49. {
  50. $this->childs = new ArrayCollection;
  51. }
  52. /**
  53. *
  54. * @return string
  55. */
  56. public function __toString()
  57. {
  58. return (string)$this->name;
  59. }
  60. /**
  61. * @return bigint
  62. */
  63. public function getId()
  64. {
  65. return $this->id;
  66. }
  67. /**
  68. * @param string $name
  69. *
  70. * @return Node
  71. */
  72. public function setName($name)
  73. {
  74. $this->name = $name;
  75. return $this;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getName()
  81. {
  82. return $this->name;
  83. }
  84. /**
  85. * @param Node $parent
  86. *
  87. * @return Node
  88. */
  89. public function setParent($parent = null)
  90. {
  91. $this->parent = $parent;
  92. return $this;
  93. }
  94. /**
  95. * @return Node
  96. */
  97. public function getParent()
  98. {
  99. return $this->parent;
  100. }
  101. /**
  102. * @param Node $childs
  103. *
  104. * @return Node
  105. */
  106. public function addChild($childs)
  107. {
  108. $this->childs[] = $childs;
  109. return $this;
  110. }
  111. /**
  112. * @param Node $childs
  113. */
  114. public function removeChild(Node $childs)
  115. {
  116. $this->childs->removeElement($childs);
  117. }
  118. /**
  119. * @return Doctrine\Common\Collections\Collection
  120. */
  121. public function getChilds()
  122. {
  123. return $this->childs;
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function getEntitiesForRemove()
  129. {
  130. $entities = [];
  131. if ($this->childs->count() != 0) {
  132. $entities['childs'] = $this->childs;
  133. }
  134. return $entities;
  135. }
  136. }