ECommerceCategory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Doctrine\Tests\Models\ECommerce;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. /**
  5. * ECommerceCategory
  6. * Represents a tag applied on particular products.
  7. *
  8. * @author Giorgio Sironi
  9. * @Entity
  10. * @Table(name="ecommerce_categories")
  11. */
  12. class ECommerceCategory
  13. {
  14. /**
  15. * @Id @Column(type="integer")
  16. * @GeneratedValue(strategy="AUTO")
  17. */
  18. private $id;
  19. /**
  20. * @Column(type="string", length=50)
  21. */
  22. private $name;
  23. /**
  24. * @ManyToMany(targetEntity="ECommerceProduct", mappedBy="categories")
  25. */
  26. private $products;
  27. /**
  28. * @OneToMany(targetEntity="ECommerceCategory", mappedBy="parent", cascade={"persist"})
  29. */
  30. private $children;
  31. /**
  32. * @ManyToOne(targetEntity="ECommerceCategory", inversedBy="children")
  33. * @JoinColumn(name="parent_id", referencedColumnName="id")
  34. */
  35. private $parent;
  36. public function __construct()
  37. {
  38. $this->products = new ArrayCollection();
  39. $this->children = new ArrayCollection();
  40. }
  41. public function getId()
  42. {
  43. return $this->id;
  44. }
  45. public function getName()
  46. {
  47. return $this->name;
  48. }
  49. public function setName($name)
  50. {
  51. $this->name = $name;
  52. }
  53. public function addProduct(ECommerceProduct $product)
  54. {
  55. if (!$this->products->contains($product)) {
  56. $this->products[] = $product;
  57. $product->addCategory($this);
  58. }
  59. }
  60. public function removeProduct(ECommerceProduct $product)
  61. {
  62. $removed = $this->products->removeElement($product);
  63. if ($removed) {
  64. $product->removeCategory($this);
  65. }
  66. }
  67. public function getProducts()
  68. {
  69. return $this->products;
  70. }
  71. private function setParent(ECommerceCategory $parent)
  72. {
  73. $this->parent = $parent;
  74. }
  75. public function getChildren()
  76. {
  77. return $this->children;
  78. }
  79. public function getParent()
  80. {
  81. return $this->parent;
  82. }
  83. public function addChild(ECommerceCategory $child)
  84. {
  85. $this->children[] = $child;
  86. $child->setParent($this);
  87. }
  88. /** does not set the owning side. */
  89. public function brokenAddChild(ECommerceCategory $child)
  90. {
  91. $this->children[] = $child;
  92. }
  93. public function removeChild(ECommerceCategory $child)
  94. {
  95. $removed = $this->children->removeElement($child);
  96. if ($removed) {
  97. $child->removeParent();
  98. }
  99. }
  100. private function removeParent()
  101. {
  102. $this->parent = null;
  103. }
  104. }