Symfony2 MongoDB多个连接错误


Symfony2 MongoDB Multiple connections error

我在Symfony2中设置MongoDB时遇到问题。

规格:

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

我在MongoDB中的两个不同捆绑包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: ~

为了使它发挥作用,我在每个文档注释中添加了数据库的名称:

/**
 * @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框架(以及各自的条令模块)。如果我没记错的话,您必须设置所有Doctrine服务,并添加新的命名空间(在您的案例中为nxtlog_conn)。

我检查了ZF2 DoctrineMongoODMModule的来源,它仍然是我记忆中的样子:如果你想建立连接,你需要一个前缀为相同名称空间的Doctrine configuration service

从您的错误消息来看,这也适用于Symfony捆绑包,尽管我在捆绑包源代码中找不到负责的位置。

服务CCD_ 3具有对不存在的服务"doctrine_mongodb.odm.nxtlog_conn_configuration"的依赖。

这基本上告诉你:我想要一个连接,但等一下,我找不到相应的配置!

试着找出如何为orm_default连接设置配置,并像wise一样设置您的配置。如果遇到另一个相同格式的错误,请查找下一个所需的服务名称,然后冲洗并重复。

哼,虽然不确定,但希望能有所帮助。这里有一个来自谷歌集团的链接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: ~