Guest User

Untitled

a guest
Jan 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. /** @Entity */
  2. class First
  3. {
  4. /** @OneToMany(targetEntity="Second", mappedBy="parent") */
  5. protected $secondList;
  6.  
  7. // access methods here
  8. public function __construct()
  9. {
  10. $this->secondList = new ArrayCollection();
  11. }
  12. }
  13.  
  14. /** @Entity */
  15. class Second
  16. {
  17. /**
  18. * @ManyToOne(targetEntity="First", inversedBy="secondList")
  19. * @JoinColumn(name="First_id", referencedColumnName="Id")
  20. */
  21. protected $parent;
  22. }
  23.  
  24. $first = new Second();
  25. $second = new First();
  26. $first->getSecond()->add($second);
  27. $em->persist($second);
  28. $em->persist($first);
  29.  
  30. In the case of bi-directional associations you have to update the fields on both sides:
  31.  
  32. $second->setParent($first);
  33.  
  34. /** @OneToMany(targetEntity = "Second", mappedBy = "parent" ) */
Add Comment
Please, Sign In to add comment