Guest User

Untitled

a guest
Nov 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. <?php
  2.  
  3. // src/Entity/Article.php
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6.  
  7. /**
  8. * @ORM\Entity()
  9. */
  10. class Article
  11. {
  12. /**
  13. * @ORM\Id()
  14. * @ORM\GeneratedValue(strategy="AUTO")
  15. * @ORM\Column(type="integer", name="id")
  16. */
  17. protected $id;
  18.  
  19. /**
  20. * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
  21. */
  22. protected $tags;
  23.  
  24. public function __construct()
  25. {
  26. $this->tags = new ArrayCollection();
  27. }
  28.  
  29. public function addTag(Tag $tag)
  30. {
  31. if ($this->tags->contains($tag)) {
  32. return;
  33. }
  34.  
  35. $this->tags->add($tag);
  36. $tag->addArticle($this);
  37. }
  38.  
  39. public function removeTag(Tag $tag)
  40. {
  41. if (!$this->tags->contains($tag)) {
  42. return;
  43. }
  44.  
  45. $this->tags->removeElement($tag);
  46. $tag->removeArticle($this);
  47. }
  48. }
  49.  
  50. // src/Entity/Tag.php
  51. use Doctrine\ORM\Mapping as ORM;
  52. use Doctrine\Common\Collections\ArrayCollection;
  53.  
  54. /**
  55. * @ORM\Entity()
  56. */
  57. class Tag
  58. {
  59. /**
  60. * @ORM\Id()
  61. * @ORM\GeneratedValue(strategy="AUTO")
  62. * @ORM\Column(type="integer", name="id")
  63. */
  64. protected $id;
  65.  
  66. /**
  67. * @ORM\ManyToMany(targetEntity="Article", mappedBy="tags")
  68. */
  69. protected $articles;
  70.  
  71. public function __construct()
  72. {
  73. $this->articles = new ArrayCollection();
  74. }
  75.  
  76. public function addArticle(Article $article)
  77. {
  78. if ($this->articles->contains($article)) {
  79. return;
  80. }
  81.  
  82. $this->articles->add($article);
  83. $article->addTag($this);
  84. }
  85.  
  86. public function removeArticle(Article $article)
  87. {
  88. if (!$this->articles->contains($article)) {
  89. return;
  90. }
  91.  
  92. $this->articles->removeElement($article);
  93. $article->removeTag($this);
  94. }
  95. }
Add Comment
Please, Sign In to add comment