[英]Symfony2 MongoDB Multiple connections error
我在Symfony2中设置MongoDB时遇到问题。
眼镜:
"Symfony": "2.6.*"
"doctrine/mongodb-odm": "1.0.*@dev",
"doctrine/mongodb-odm-bundle": "3.0.*@dev"
我在MongoDB中有2个数据库用于2个不同的捆绑软件nxtlog和nxtsurvey。 我最初遇到的问题是未考虑在选项中添加的数据库名称,这导致使用数据库“默认”,这当然不存在。 我也不想添加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: ~
为了使其工作,我在每个Document批注中添加了数据库的名称:
/**
* @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".
因此,我发现连接和数据管理器不能使用不同的名称。 我不相信,所以我用谷歌搜索了,有人遇到了类似的问题,答案是在doctrine_mongodb下添加以下内容:
default_commit_options: ~
但是此解决方案对我不起作用,经过更多的搜索之后,我发现写捆绑包(或捆绑包的一部分)的人jmikola犯了一个错误,他说他已将其修复,并且default_commit_options不应为必需的配置选项。 (参考https://github.com/doctrine/DoctrineMongoDBBundle/issues/222 )
此时,我需要一些帮助,因为这花费了太多时间来解决。
谢谢
相当早以前,尽管我当时使用Zend Framework(以及相应的Doctrine模块),但我也尝试建立多个Doctrine连接。 如果我没记错的话,您必须设置所有 Doctrine服务并添加新的名称空间(在您的情况下为nxtlog_conn
)。
我检查了ZF2 DoctrineMongoODMModule的来源,它仍然是我记得的方式:如果要建立连接,则需要以相同名称空间为前缀的Doctrine configuration service
。
从您的错误消息来看,这也适用于Symfony捆绑软件,尽管我在捆绑软件源代码中找不到负责的位置。
服务
"doctrine_mongodb.odm.nxtlog_conn_connection"
具有对不存在的服务"doctrine_mongodb.odm.nxtlog_conn_configuration"
的依赖。
这基本上告诉您:我想要一个连接,但是请稍等,我找不到对应的配置!
尝试查找如何为orm_default
连接设置配置,并明智地设置配置。 如果遇到其他相同格式的错误,请寻找下一个所需的服务名称,然后冲洗并重复。
嗡嗡声尝试不确定,但希望会有所帮助。 此处来自Google组的链接https://groups.google.com/d/msg/doctrine-user/6YCVAZ4h4nA/YrZNfSopmNUJ
doctrine_mongodb:
default_database: "%nxtlog_database_name%"
default_connection: nxtlog_conn
default_document_manager: nxtlog_conn
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_conn:
connection: nxtlog_conn
mappings:
NxtLogBundle: ~
nxtsurvey_conn:
connection: nxtsurvey_conn
mappings:
NxtVibeSurveyBundle: ~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.