2014-01-29 2 views
0

symfony2와 Doctrine ODM을 사용합니다.Doctrine odm reference 많은 replicate 참조

나는 출판물이있다.

...\Publication: 
    fields: 
     id: 
      id: true 
      strategy: INCREMENT 
     dateDebut: 
      type: date 
     dateFin: 
      type: date 

나는 문서 podcast를 referenceMany로 가지고 있습니다.

db.Podcast.find({'_id':2}) 

이 결과 :이 요청을 소성

...\Podcast: 
    fields: 
     id: 
      id: true 
      strategy: INCREMENT 
    referenceMany: 
     publications: 
      targetDocument: ...\Publication 
      cascade: all 

.

db.Podcast.find({'_id':2}) 

이 결과 : 나 persit 및 팟 캐스트를 씻고 난 요청 소성

{ "_id" : 2, 
... 
"publications" : [{"$ref" : "Publication","$id" : 3}] 
... 
} 

.

{ "_id" : 2, 
... 
"publications" : [ 
    {"$ref" : "Publication","$id" : 3}, 
    {"$ref" : "Publication","$id" : 3} 
] 
... 
} 

왜 참조가 중복되는가?

답변

0

해결책을 찾았습니다. 플러시하기 전에 게시 컬렉션을 정리해야합니다. 그것은 최적이 아니지만 작동합니다.

{ "_id" : 2, 
... 
"publications" : [{"$ref" : "Publication","$id" : 3}] 
... 
} 

내가 문서 팟 캐스트가 :은 BD에서

내 저장소에서 서비스

public function attachPodcast($podcast) 
{ 
    .... 
    $podcast->setPublications(new ArrayCollection($publications)); 
    $this->getRepo()->attach($podcast); 
} 

에서

use Doctrine\Common\Collections\ArrayCollection; 
class Podcast implements InterfaceMongoDocument 
{ 
    ...... 
    private $publications; 
    ...... 

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

....... 
} 

public function attach(InterfaceMongoDocument $document) 
{ 
     $documentToClearCollection = clone $document; 
     $documentToClearCollection->getPublications()->clear(); 
     $this->entiteManager->persist($document); 
     $this->entiteManager->flush(); 
} 

그리고 결국

{ "_id" : 2, 
... 
"publications" : [{"$ref" : "Publication","$id" : 3}] 
... 
} 

에서당신이 할 수있는 다른 방법을 알고 있나요 ????

관련 문제