2016-09-23 5 views
-1

OneToMany側から操作を処理する方法はわかりません。doctrine-symfonyのOneToManyリレーション操作

オブジェクトAはManyToOne側にあり、オブジェクトBはOneToMany側にあります。

  1. オブジェクトBを作成するとき、私はそれに多くのオブジェクトを割り当てるには、したい - 削除するとき、この中に私の解決策は、Aのオブジェクトをフェッチし、それらに割り当てる

  2. Bの目的であった私が欲しいですTHER私はおそらく

ですテーブルの上にondelete="setNull" 機能を追加する必要があることを見るの研究から - オブジェクトBは、オブジェクト Aからすべての参照をnullに設定しますこの状況に対処するためのより良い方法(または一般的な方法)?

+0

明瞭にするために再フォーマットしました。 –

答えて

0
use Doctrine\ORM\Mapping as ORM; 

class AObject 
{ 
    // ... 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\BObject", inversedBy="a", onDelete="SET NULL") 
    */ 
    private $b; 
} 

class BObject 
{ 
    // ... 

    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\AObject", mappedBy="b", cascade={"persist"}) 
    */ 
    private $a; 

    public function __construct() 
    { 
     $this->a = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * @return \Doctrine\Common\Collections\ArrayCollection 
    */ 
    public getA() 
    { 
     return $this->a; 
    } 

    /** 
    * @param \Doctrine\Common\Collections\ArrayCollection $a 
    */ 
    public setA($a) 
    { 
     $this->a = $a; 
    } 

    /** 
    * @param AObject $a 
    * @return BObject 
    */ 
    public addA($a) 
    { 
     $this->a[] = $a; 
     $a->setB($this); // assign B object to A 

     return $this; // For method chaining 
    } 

    /** 
    * @param AObject $a 
    */ 
    public removeA($a) 
    { 
     $this->a->removeElement($a); 
    } 
} 

あなたはまだオブジェクトをフェッチする必要がありますが、あなたのコードは、どのAオブジェクトが新しいBオブジェクトに割り当てるか魔法のようには知ることができません。

上で定義したクラスBを使用すると、

$b = new BObject(); 
$b 
    ->addA($a1) 
    ->addA($a2) 
    ->addA(new AObject()); 
$entityManager->persist($b); 
$entityManager->flush(); 

を書くことができますし、オブジェクトが2の場合B.

への参照を持つことになります)、それはまだ、より良い解決策だ、データベースのレベルにそれを扱います。

0

doctrine lifecycle eventsをご利用ください。サンプルについて

、まずあなたがイベントリスナーを定義する必要があります。代替として、あなたはすべての子entitesを除去するため、Doctrine cascade operationsを使用することができ、また

// FooBundle\EventListener\BarSubscriber.php 
namespace FooBundle\EventListener; 

use Doctrine\Common\EventSubscriber; 
use Doctrine\ORM\Event\LifecycleEventArgs; 

class BarSubscriber implements EventSubscriber 
{ 
    public function getSubscribedEvents() 
    { 
     return array(
      'preRemove', 
      'postPersist', 
     ); 
    } 

    // This method will be executed on each entity, before it has been deleted 
    // You can do here what u want in your second paragraph 
    public function preRemove(LifecycleEventArgs $args) 
    { 
     $entity = $args->getEntity(); 

     // Do not forget to check your entity type 
     if (!$entity instanceof SomeInterface) { 
      return; 
     } 

     // Some code that handle some actions on related entites 
    } 

    // This code will be executed after each entity creation 
    // Here you can add more relations for your main entity 
    public function postPersist(LifecycleEventArgs $args) 
    { 
     $entity = $args->getEntity(); 
     if (!$entity instanceof TimestampableInterface) { 
      return; 
     } 

     // Some actions on relations, after the establishment of the main object 
    } 
} 

:1を作成し、その後

# /src/FooBundle/Resources/config/services.yml 
services: 

# Event subscribers 
foo.bar.subscriber: 
    class: FooBundle\EventListener\BarSubscriber 
    tags: 
     - { name: doctrine.event_subscriber } 

関連する問題