Symfony2 MongoDB Multiple connections error
Asked Answered
S

2

6

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

Squarrose answered 2/3, 2015 at 14:13 Comment(0)
S
1

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.

Spooky answered 6/3, 2015 at 16:18 Comment(0)
G
1

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: ~
Getz answered 7/3, 2015 at 15:27 Comment(4)
That's exactly what I have tried before, except that I do not want default parameters, as for my case it would be completely useless. Why would I use the default db or connection if I have 2 bundles using 2 different DB's, as both bundles have a completely different logic, and do not depend on each other.Squarrose
yes i understand you don't want a default parameters but you have to know that default doesn't mean the odm will use it automatically. You're the one who choose which connexions you use.Getz
well, it actually does select the default one all the time for some reason. And so far, the only way I found was to add the db name in the annotations, which is not a good solution for my use case. I xdebugged it, and saw that db from the connection options was actually being taken, but not used at the end. I haven't been able to figure out why just yet.Squarrose
Try in some controller in both bundles see what you get var_dump($this->container->getService('doctrine.odm.mongodb.document_manager'));Getz

© 2022 - 2024 — McMap. All rights reserved.