I use symfony2 and Doctrine ODM.
I have a document Publication.
...\Publication:
fields:
id:
id: true
strategy: INCREMENT
dateDebut:
type: date
dateFin:
type: date
I have a document podcast whit a referenceMany.
...\Podcast:
fields:
id:
id: true
strategy: INCREMENT
referenceMany:
publications:
targetDocument: ...\Publication
cascade: all
When i fired this request :
db.Podcast.find({'_id':2})
this is the result.
{ "_id" : 2,
...
"publications" : [{"$ref" : "Publication","$id" : 3}]
...
}
When i persit and flush the podcast and i fired this request:
db.Podcast.find({'_id':2})
this is the result.
{ "_id" : 2,
...
"publications" : [
{"$ref" : "Publication","$id" : 3},
{"$ref" : "Publication","$id" : 3}
]
...
}
Why the reference are duplicate ????
I found the solution. I have to clear the publication collection before the flush. It's not optimal but it should work.
In the bd :
{ "_id" : 2,
...
"publications" : [{"$ref" : "Publication","$id" : 3}]
...
}
I have a document podcast :
use Doctrine\Common\Collections\ArrayCollection;
class Podcast implements InterfaceMongoDocument
{
......
private $publications;
......
public function __construct()
{
$this->publications = new ArrayCollection();
}
.......
}
In the service
public function attachPodcast($podcast)
{
....
$podcast->setPublications(new ArrayCollection($publications));
$this->getRepo()->attach($podcast);
}
In my repository
public function attach(InterfaceMongoDocument $document)
{
$documentToClearCollection = clone $document;
$documentToClearCollection->getPublications()->clear();
$this->entiteManager->persist($document);
$this->entiteManager->flush();
}
And in the end
{ "_id" : 2,
...
"publications" : [{"$ref" : "Publication","$id" : 3}]
...
}
Do you know another way to do this????
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.