繁体   English   中英

Doctrine odm 参考许多复制参考

[英]Doctrine odm referenceMany replicate references

我使用 symfony2 和 Doctrine ODM。

我有一个文件出版物。

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

我有一个包含 referenceMany 的文档播客。

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

当我发起这个请求时:

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

这就是结果。

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

当我坚持并刷新播客并发出此请求时:

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

这就是结果。

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

为什么引用是重复的????

我找到了解决方案。 我必须在刷新之前清除出版物集合。 这不是最佳的,但它应该工作。

在 bd 中:

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

我有一个文档播客:

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

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

.......
}

在服务中

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

在我的存储库中

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}]
...
}

你知道另一种方法吗????

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM