[英]Symfony2 MongoDB Multiple connections error
我在Symfony2中設置MongoDB時遇到問題。
眼鏡:
"Symfony": "2.6.*"
"doctrine/mongodb-odm": "1.0.*@dev",
"doctrine/mongodb-odm-bundle": "3.0.*@dev"
我在MongoDB中有2個數據庫用於2個不同的捆綁軟件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: ~
為了使其工作,我在每個Document批注中添加了數據庫的名稱:
/**
* @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 Framework(以及相應的Doctrine模塊),但我也嘗試建立多個Doctrine連接。 如果我沒記錯的話,您必須設置所有 Doctrine服務並添加新的名稱空間(在您的情況下為nxtlog_conn
)。
我檢查了ZF2 DoctrineMongoODMModule的來源,它仍然是我記得的方式:如果要建立連接,則需要以相同名稱空間為前綴的Doctrine configuration service
。
從您的錯誤消息來看,這也適用於Symfony捆綁軟件,盡管我在捆綁軟件源代碼中找不到負責的位置。
服務
"doctrine_mongodb.odm.nxtlog_conn_connection"
具有對不存在的服務"doctrine_mongodb.odm.nxtlog_conn_configuration"
的依賴。
這基本上告訴您:我想要一個連接,但是請稍等,我找不到對應的配置!
嘗試查找如何為orm_default
連接設置配置,並明智地設置配置。 如果遇到其他相同格式的錯誤,請尋找下一個所需的服務名稱,然后沖洗並重復。
嗡嗡聲嘗試不確定,但希望會有所幫助。 此處來自Google組的鏈接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: ~
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.