Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Entity
- */
- class A
- {
- /**
- * @OneToMany(targetEntity="B", mappedBy="a", cascade={"PERSIST"})
- */
- protected $bCollection;
- public function addB(B $b)
- {
- // update both sides of the relationship
- $b->setA($this);
- $this->bCollection->add($b);
- }
- }
- /**
- * @Entity
- */
- class B
- {
- /**
- * @ManyToOne(targetEntity="A", inversedBy="bCollection")
- */
- protected $a;
- public function setA(A $a)
- {
- $this->a = $a;
- }
- }
- // Usage
- $a = new A();
- $b1 = new B();
- $b2 = new B();
- $a->addB($b1);
- $a->addB($b2);
- $em->persist($a);
- /* Without the cascade={"PERSIST"} we have to do this
- $em->persist($b1);
- $em->persist($b2);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement