2016-07-16 7 views
6

私はSymfony 3.1とDoctrine 2.5を使っています。Doctrine manyToManyはArrayCollectionの代わりにPersistentCollectionを返します。

私のセットアップ私はいつもそうであるように多対多の関係:

manyToMany: 
     placeServices: 
      targetEntity: Acme\MyBundle\Entity\PlaceService 
      joinTable: 
       name: place_place_service 
       joinColumns: 
        place_id: 
         referencedColumnName: id 
       inverseJoinColumns: 
        place_service_id: 
         referencedColumnName: id 

そして、私は私のエンティティをロードする際のものは、教義はPersistentCollectionを入れて

protected $placeServices; 

    ... 

    public function __construct() 
    { 
     $this->placeServices = new ArrayCollection(); 
    } 

    ... 

    /** 
    * @return ArrayCollection 
    */ 
    public function getPlaceServices(): ArrayCollection 
    { 
     return $this->placeServices; 
    } 

    /** 
    * @param PlaceServiceInterface $placeService 
    * @return PlaceInterface 
    */ 
    public function addPlaceService(PlaceServiceInterface $placeService): PlaceInterface 
    { 
     if(!$this->placeServices->contains($placeService)) { 
      $this->placeServices->add($placeService); 
     } 

     return $this; 
    } 

    /** 
    * @param PlaceServiceInterface $placeService 
    * @return PlaceInterface 
    */ 
    public function removePlaceService(PlaceServiceInterface $placeService): PlaceInterface 
    { 
     if($this->placeServices->contains($placeService)) { 
      $this->placeServices->removeElement($placeService); 
     } 

     return $this; 
    } 

私のエンティティにメソッドを追加$ this-> placeServicesプロパティに追加します。これは大きな問題のようには思えませんが、$ form-> handleRequest()がトリガされたときに、Doctrineが新しいデータを挿入しようとすると、それらの2つのエンティティ(symfonyフォームタイプの単純な複数のチェックボックス)私のエンティティでは、get/add/removeメソッドがArrayCollectionを使用していないとエラーをスローします。

私は、(unwrapメソッドを使用して)PersistentCollectionをArrayCollectionに変換するようにgetter/add/removeメソッドを強制できますが、作成された関係は保持されません。

fetch = "EAGER"を関係に設定して、プロパティがArrayCollectionで初期化され、リレーションが保持されている場合、回避策が見つかりました。しかし、私はそれが良い解決策であるとは思わない。

+0

symfonyフォームと正確なエラーメッセージで更新できますか? – galeaspablo

答えて

15

:)

おかげだけではなく、のArrayCollection教義\共通\コレクション\コレクションインタフェースを使用しています。 ArrayCollectionおよびPersistentCollectionこのインターフェイスを実装します。

Doctrineは、レイジーローディングエンティティのためにをPersistentCollectionに使用します。あなたは正しい、EAGERを使って常に良い解決策ではない - それは性能問題を引き起こす可能性がある。

+0

ありがとう、私は考えを得る。 –

関連する問題