简体   繁体   English

Symfony2 MongoDB多个连接错误

[英]Symfony2 MongoDB Multiple connections error

I have an issue setting up MongoDB in Symfony2. 我在Symfony2中设置MongoDB时遇到问题。

Specs: 眼镜:

"Symfony": "2.6.*"
"doctrine/mongodb-odm": "1.0.*@dev",
"doctrine/mongodb-odm-bundle": "3.0.*@dev"

I have 2 databases used in 2 different bundles, nxtlog and nxtsurvey, in MongoDB. 我在MongoDB中有2个数据库用于2个不同的捆绑软件nxtlog和nxtsurvey。 The original issue I had was that the db name I added in options was not taken into account, which resulted in database 'default' to be used, which of course does not exist. 我最初遇到的问题是未考虑在选项中添加的数据库名称,这导致使用数据库“默认”,这当然不存在。 I also do not want to add default_connection and default_manager, nor even default_database as both connections are used in not-core bundles. 我也不想添加default_connection和default_manager,甚至不想default_database,因为这两个连接都在非核心包中使用。

==== Attempt #1 ==== ====尝试#1 ====

Here is the original config I had: 这是我原来的配置:

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: ~

In order to make it work, I added the name of the db in each Document annotations: 为了使其工作,我在每个Document批注中添加了数据库的名称:

/**
 * @MongoDB\Document(db="nxtlog")
 */
class ErrorLogs

Which is a temporary solution, but since my plan is to reuse the bundles in my other projects, I do not want to have to go through all documents and set the name of the db. 这是一个临时解决方案,但是由于我的计划是在其他项目中重用捆绑软件,因此我不想遍历所有文档并设置数据库名称。

==== Attempt #2 ==== ====尝试#2 ====

My second attempt was to rigorously follow the documentation, and therefore I tried the following: 我的第二次尝试是严格遵循文档,因此我尝试了以下操作:

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: ~

And get the following error: 并得到以下错误:

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".

So I figured out that I could not have different names for connections and data-managers. 因此,我发现连接和数据管理器不能使用不同的名称。 Which I did not believe, so I googled it, and someone had a similar issue, and the answer was to add the following under doctrine_mongodb: 我不相信,所以我用谷歌搜索了,有人遇到了类似的问题,答案是在doctrine_mongodb下添加以下内容:

default_commit_options: ~

But this solution did not work for me, and after more googling, I found out that jmikola, the guy who wrote the bundle (or parts of it), made a mistake, he said he fixed it, and that default_commit_options should not be a required configuration option. 但是此解决方案对我不起作用,经过更多的搜索之后,我发现写捆绑包(或捆绑包的一部分)的人jmikola犯了一个错误,他说他已将其修复,并且default_commit_options不应为必需的配置选项。 (ref. https://github.com/doctrine/DoctrineMongoDBBundle/issues/222 ) (参考https://github.com/doctrine/DoctrineMongoDBBundle/issues/222

At this point I would need some help, because this is taking far too much time to solve. 此时,我需要一些帮助,因为这花费了太多时间来解决。

Thanks 谢谢

Quite some time ago I tried to setup multiple Doctrine connections, too, although I used Zend Framework (and the respective Doctrine modules) at the time. 相当早以前,尽管我当时使用Zend Framework(以及相应的Doctrine模块),但我也尝试建立多个Doctrine连接。 If I remember correctly, you have to setup all Doctrine services with the new namespace added (in your case nxtlog_conn ). 如果我没记错的话,您必须设置所有 Doctrine服务并添加新的名称空间(在您的情况下为nxtlog_conn )。

I checked the source of the ZF2 DoctrineMongoODMModule and it is still how I remembered it: if you want to have the connection, you need a Doctrine configuration service prefixed with the same namespace. 我检查了ZF2 DoctrineMongoODMModule来源,它仍然是我记得的方式:如果要建立连接,则需要以相同名称空间为前缀的Doctrine configuration service

Judging from your error message, this also applies to the Symfony bundle, albeit I couldn't find the responsible location in the bundles source code. 从您的错误消息来看,这也适用于Symfony捆绑软件,尽管我在捆绑软件源代码中找不到负责的位置。

The service "doctrine_mongodb.odm.nxtlog_conn_connection" has a dependency on a non-existent service "doctrine_mongodb.odm.nxtlog_conn_configuration" . 服务"doctrine_mongodb.odm.nxtlog_conn_connection"具有对不存在的服务"doctrine_mongodb.odm.nxtlog_conn_configuration"的依赖。

This basically tells you: I want a connection, but wait a second, I can't find the corresponding configuration! 这基本上告诉您:我想要一个连接,但是请稍等,我找不到对应的配置!

Try to find how the configuration is set up for the orm_default connection and set up your configuration like wise. 尝试查找如何为orm_default连接设置配置,并明智地设置配置。 If you encounter another error of the same format, hunt the next required service name and then rinse and repeat. 如果遇到其他相同格式的错误,请寻找下一个所需的服务名称,然后冲洗并重复。

Hum try not sure but hope it will help. 嗡嗡声尝试不确定,但希望会有所帮助。 Here a link from google group https://groups.google.com/d/msg/doctrine-user/6YCVAZ4h4nA/YrZNfSopmNUJ 此处来自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.

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