Doctrine mongo ODM在Symfony中复制嵌入式文档

时间:2013-04-28 20:13:29

标签: mongodb symfony doctrine phpunit doctrine-odm

在Symfony 2.2.1中使用Doctrine mongo ODM 1.0.0-BETA8,我遇到了CRUD更新部分的麻烦。在persist()后,新的嵌入文档将被复制。

我运行了与doctrine-odm附带的嵌入式文档相关的phpunit测试,它们似乎工作正常。我注意到的一件事是Doctrine测试文档与我的文档之间存在差异,即嵌入文档中存在id。据我所知,子文档中的id是不必要的,这可以通过测试套件User和Phonenumber中的Documents来证明。 Phonenumber没有id,测试通过。

我正在从yaml生成我的文档:

App\AppBundle\Document\Lexicon:
    db: somedb
    collection: somecollection
    fields:
      id:
        type: id
        id: true
      interpretations:
        embedded: true
        type: many
        targetDocument: App\AppBundle\Document\Interpretation

App\AppBundle\Document\Interpretation:
  type: embeddedDocument #this looks like a good thing to do
  fields:
    #id:    #when this is commented out, Symfony throws an error
    #  type: id
    #  id: true
    explanation: 
      type: string

我不想要解释ID,但没有它,symfony抱怨没有为Document 指定标识符/主键。虽然我怀疑这是导致问题的原因。

    $entity = $dm->getRepository('App\AppBundle\Document\Lexicon')->find($id);

    $int = new Interpretation(); 
    $int->setExplanation('first'); 
    $entity->addInterpretation($int);

    $dm->persist($entity);
    $dm->flush();

这导致Lexicon有2个解释:

{
  "explanation": "first"
},
{
  "_id": ObjectId("517d8040cbad49f001000004"),
  "explanation": "first"
}

编辑:我找到type: embeddedDocument指令来替换id节。记得同步所有内容:

php app/console doctrine:mongodb:generate:documents AppAppBundle 
php app/console doctrine:mongodb:generate:hydrators

然而,仍然看到重复。

0 个答案:

没有答案
相关问题