6

我在 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

在这一点上,我需要一些帮助,因为这需要太多时间来解决。

谢谢

4

2 回答 2

1

嗯,不确定,但希望它会有所帮助。这是来自谷歌组的链接 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: ~
于 2015-03-07T15:27:32.337 回答
1

很久以前,我也尝试设置多个 Doctrine 连接,尽管当时我使用了 Zend Framework(和相应的 Doctrine 模块)。如果我没记错的话,您必须设置所有Doctrine 服务并添加新的命名空间(在您的情况下nxtlog_conn)。

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

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

该服务"doctrine_mongodb.odm.nxtlog_conn_connection"依赖于一个不存在的服务"doctrine_mongodb.odm.nxtlog_conn_configuration"

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

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

于 2015-03-06T16:18:56.460 回答