Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. /**
  2.  * @Entity
  3.  */
  4. class A
  5. {
  6.     /**
  7.      * @OneToMany(targetEntity="B", mappedBy="a", cascade={"PERSIST"})
  8.      */
  9.     protected $bCollection;
  10.  
  11.     public function addB(B $b)
  12.     {
  13.         // update both sides of the relationship
  14.         $b->setA($this);
  15.         $this->bCollection->add($b);
  16.     }
  17. }
  18.  
  19. /**
  20.  * @Entity
  21.  */
  22. class B
  23. {
  24.     /**
  25.      * @ManyToOne(targetEntity="A", inversedBy="bCollection")
  26.      */
  27.     protected $a;
  28.  
  29.     public function setA(A $a)
  30.     {
  31.         $this->a = $a;
  32.     }
  33. }
  34.  
  35.  
  36. // Usage
  37. $a = new A();
  38. $b1 = new B();
  39. $b2 = new B();
  40.  
  41. $a->addB($b1);
  42. $a->addB($b2);
  43.  
  44. $em->persist($a);
  45. /* Without the cascade={"PERSIST"} we have to do this
  46. $em->persist($b1);
  47. $em->persist($b2);
  48.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement