简体   繁体   English

原则MongoDB ODM不持久

[英]Doctrine MongoDB ODM does not persist

I'm trying to start using MongoDB with Doctrine MongoDB ODM 1.1.3 and Laravel 5.4. 我正在尝试将MongoDB与Doctrine MongoDB ODM 1.1.3和Laravel 5.4一起使用。 Everything had been going fine before I removed the database called "doctrine" manually (the default database name I guess) in order to clean up the rubbish in it, so basically I just wanted to remove the database and was hoping that Doctrine will create a new one. 在我手动删除名为“ doctrine”的数据库(我猜是默认的数据库名称)以清理其中的垃圾之前,一切都进行得很好,所以基本上我只是想删除数据库,并希望Doctrine将创建一个新的一个。 Now when I'm trying to call 现在,当我试图打电话给

$mgr->persist($divRoot);
$mgr->flush();

It assigns an ID to the $divRoot object, but doesn't persist it. 它为$ divRoot对象分配一个ID,但不持久化。 Ie when I then call the findAll() method on the repo, it returns nothing. 也就是说,当我然后在findAll()上调用findAll()方法时,它什么也不返回。 And it doesn't create any databases anymore. 而且它不再创建任何数据库。 The $divRoot has changing fields every time I'm trying to save it. 每次我尝试保存$ divRoot时,其字段都会更改。 I've got really stuck, please help 我真的被卡住了,请帮忙

UPDATE 1 更新1

If I initialize a new DocumentManager specifying a new path to the documents ( AnnotationDriver::create($documents) ), the ODM works normally persisting and retrieving the documents. 如果我初始化一个新的DocumentManager,指定一个新的文档路径( AnnotationDriver::create($documents) ),则ODM会正常工作,以持久化和检索文档。

I've figured out what was wrong. 我已经找出了问题所在。 I was working only with embedded documents whereas it must be at least one root Document. 我只使用嵌入式文档,而它必须至少是一个根文档。 Originally all of them were marked as Documents that's why I was able to persist them, it's nothing to do with the database removal. 最初所有它们都被标记为Documents,这就是为什么我能够保留它们的原因,与数据库删除无关。

So, I was trying to persist a composite consisting out EmbeddedDocuments only. 因此,我试图保留仅包含EmbeddedDocuments的复合文件。

The solution was to create a root wrapper for the composite marked as Document. 解决方案是为标记为Document的组合创建根包装。

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

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