【问题标题】:Doctrine odm referenceMany replicate referencesDoctrine odm referenceMany 复制引用
【发布时间】:2023-03-23 04:55:01
【问题描述】:

我使用 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}
]
...
}

为什么引用重复???

【问题讨论】:

    标签: mongodb symfony doctrine-orm doctrine-odm


    【解决方案1】:

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

    在卧室:

    { "_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}]
    ...
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 2014-03-20
      相关资源
      最近更新 更多