Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 MongoDB Multiple connections error

I have an issue setting up MongoDB in Symfony2.

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

==== Attempt #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:

/**
 * @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 ====

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:

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. (ref. 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

like image 356
nicom974 Avatar asked Mar 02 '15 14:03

nicom974


2 Answers

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. If I remember correctly, you have to setup all Doctrine services with the new namespace added (in your case 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.

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.

The service "doctrine_mongodb.odm.nxtlog_conn_connection" has a dependency on a non-existent service "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. If you encounter another error of the same format, hunt the next required service name and then rinse and repeat.

like image 153
Fabian Keller Avatar answered Oct 05 '22 06:10

Fabian Keller


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

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: ~
like image 40
Marcel Djaman Avatar answered Oct 05 '22 07:10

Marcel Djaman