Guest User

Untitled

a guest
Feb 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. class Node
  2. {
  3. /** [...]
  4. * @ORMColumn(name="id", type="integer", nullable=false)
  5. * @ORMId
  6. * @ORMGeneratedValue(strategy="IDENTITY")
  7. */
  8. private $id;
  9.  
  10. /** [...]
  11. * @ORMColumn(name="name", type="string", length=100, nullable=false)
  12. */
  13. private $name;
  14.  
  15. /** [...]
  16. * @ORMManyToOne(targetEntity="Node", inversedBy="children")
  17. */
  18. private $parent;
  19.  
  20. /** [...]
  21. * @ORMOneToMany(targetEntity="Node", mappedBy="parent")
  22. */
  23. private $children;
  24.  
  25. // ...
  26.  
  27. public function addChild(Node $child)
  28. {
  29. $this->children[] = $child;
  30. $child->setParent($this);
  31.  
  32. return $this;
  33. }
  34.  
  35. public function removeChild(Node $child)
  36. {
  37. $this->children->removeElement($child);
  38. $child->setParent(null);
  39. }
  40.  
  41. // ...
  42.  
  43. AppBundleEntityNode:
  44. Node-0:
  45. name: 'Trunk'
  46. Node-1:
  47. name: 'Branch 1'
  48. parent: '@Node-0'
  49. Node-2:
  50. name: 'Branch 2'
  51. parent: '@Node-0'
  52.  
  53. $loader = new NativeLoader();
  54. $fixtures = $loader->loadFile('node.yml')->getObjects();
  55. echo $fixtures['Node-1']->getParent()->getName();
  56.  
  57. echo count($fixtures['Node-0']->getChildren());
  58.  
  59. AppBundleEntityNode:
  60. Node-0:
  61. name: 'Trunk'
  62. children: ['@Node-1', '@Node-2']
  63. Node-1:
  64. name: 'Branch 1'
  65. Node-2:
  66. name: 'Branch 2'
  67.  
  68. public function addChild(Node $child)
  69. {
  70. $this->children[] = $child;
  71. $child->setParent($this);
  72.  
  73. return $this;
  74. }
  75.  
  76. public function removeChild(Node $child)
  77. {
  78. $this->children->removeElement($child);
  79. $child->setParent(null);
  80. }
  81.  
  82. AppBundleEntityNode:
  83. Node-0:
  84. name: 'Trunk'
  85. Node-1:
  86. name: 'Branch 1'
  87. parent: '@Node-0'
  88. Node-2:
  89. name: 'Branch 2'
  90. parent: '@Node-0'
  91.  
  92. public function setParent(Node $parent)
  93. {
  94. $parent->addChild($this);
  95. $this->parent = $parent;
  96.  
  97. return $this;
  98. }
Add Comment
Please, Sign In to add comment