Symfony2 MongoDB Multiple Connection Error

I have a problem setting up MongoDB in Symfony2.

Specifications:

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

I have 2 databases used in two different packages, nxtlog and nxtsurvey, in MongoDB. The original problem was that the db name that I added in the parameters was not taken into account, which led to the use of the default database, which, of course, does not exist. I also do not want to add default_connection and default_manager, and even default_database, since both connections are used in non-core packages.

==== Attempt # 1 ====

Here is the original configuration 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: ~ 

To make it work, I added the db name to each document annotation:

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

This is a temporary solution, but since my plan is to reuse packages in my other projects, I do not want to go through all the documents and set the db name.

==== Attempt # 2 ====

My second attempt was to strictly follow the documentation, and so 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 realized that I cannot have different names for connections and data managers. Which I did not believe, so I was looking for it, and someone had a similar problem, and the answer was to add the following to doctrine_mongodb:

 default_commit_options: ~ 

But this solution did not work for me, and after more searches, I found out that jmikola, the guy who wrote the kit (or parts of it) made a mistake, he said that he fixed it, and that default_commit_options should not be a required configuration . (link https://github.com/doctrine/DoctrineMongoDBBundle/issues/222 )

At this point, I will need help because it takes too much time.

thanks

+6
source share
2 answers

Quite a long time ago I tried to establish several Doctrine connections, although at that time I used the Zend Framework (and the corresponding Doctrine modules). If I remember correctly, you need to configure all Doctrine services with the new namespace added (in your case nxtlog_conn ).

I checked the source of the ZF2 DoctrineMongoODMModule and I still remember it: if you want to connect, you need a Doctrine configuration service with a prefix with the same namespace.

Judging by your error message, this also applies to the Symfony package, although I could not find a responsible place in the package source code.

The "doctrine_mongodb.odm.nxtlog_conn_connection" service has a dependency on the "doctrine_mongodb.odm.nxtlog_conn_configuration" service.

This basically tells you: I want a connection, but wait a second, I can’t find the appropriate configuration!

Try to find out how the configuration is configured to connect orm_default , and configure your configuration as wise. If you encounter another error of the same format, find the next required service name, then flush and retry.

+1
source

Hum is not sure, but hope this helps. Here is a link from the 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: ~ 
+1
source

Source: https://habr.com/ru/post/983222/


All Articles