我在 Symfony2 中设置 MongoDB 时遇到问题。
眼镜:
"Symfony": "2.6.*"
"doctrine/mongodb-odm": "1.0.*@dev",
"doctrine/mongodb-odm-bundle": "3.0.*@dev"
我在 MongoDB 中的 2 个不同的包 nxtlog 和 nxtsurvey 中使用了 2 个数据库。我最初遇到的问题是我在选项中添加的数据库名称没有被考虑在内,这导致使用数据库“默认”,这当然不存在。我也不想添加 default_connection 和 default_manager,甚至也不想添加 default_database,因为这两个连接都在非核心包中使用。
==== 尝试 #1 ====
这是我原来的配置:
doctrine_mongodb:
connections:
nxtlog:
server: "%nxtlog_database_server%"
options:
username: "%nxtlog_database_username%"
password: "%nxtlog_database_password%"
db: "%nxtlog_database_name%"
nxtsurvey:
server: "%nxtsurvey_database_server%"
options:
username: "%nxtsurvey_database_username%"
password: "%nxtsurvey_database_password%"
db: "%nxtsurvey_database_name%"
document_managers:
nxtlog:
mappings:
NxtLogBundle: ~
nxtsurvey:
mappings:
NxtVibeSurveyBundle: ~
为了使它工作,我在每个文档注释中添加了数据库的名称:
/**
* @MongoDB\Document(db="nxtlog")
*/
class ErrorLogs
这是一个临时解决方案,但由于我的计划是在我的其他项目中重用捆绑包,我不想浏览所有文档并设置数据库的名称。
==== 尝试 #2 ====
我的第二次尝试是严格遵循文档,因此我尝试了以下方法:
doctrine_mongodb:
connections:
nxtlog_conn:
server: "%nxtlog_database_server%"
options:
username: "%nxtlog_database_username%"
password: "%nxtlog_database_password%"
connect: true
db: "%nxtlog_database_name%"
nxtsurvey_conn:
server: "%nxtsurvey_database_server%"
options:
username: "%nxtsurvey_database_username%"
password: "%nxtsurvey_database_password%"
connect: true
db: "%nxtsurvey_database_name%"
document_managers:
nxtlog_dm:
connection: nxtlog_conn
mappings:
NxtLogBundle: ~
nxtsurvey_dm:
connection: nxtsurvey_conn
mappings:
NxtVibeSurveyBundle: ~
并得到以下错误:
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58:
The service "doctrine_mongodb.odm.nxtlog_conn_connection" has a dependency on a non-existent service "doctrine_mongodb.odm.nxtlog_conn_configuration".
所以我发现连接和数据管理器不能有不同的名称。我不相信,所以我google了一下,有人有类似的问题,答案是在doctrine_mongodb下添加以下内容:
default_commit_options: ~
但是这个解决方案对我不起作用,经过更多谷歌搜索后,我发现编写捆绑包(或部分捆绑包)的人 jmikola 犯了一个错误,他说他修复了它,并且 default_commit_options 不应该是所需的配置选项。(参考https://github.com/doctrine/DoctrineMongoDBBundle/issues/222)
在这一点上,我需要一些帮助,因为这需要太多时间来解决。
谢谢