一、在app/config/parameters.ini里配置
; These parameters can be imported into other config files ; by enclosing the key with % (like %database_user%) ; Comments start with ';', as in php.ini [parameters] database_driver = pdo_mysql database_host = 192.168.0.1 database_port = database_name = sym database_user = root database_password = test database2_host = 192.168.0.2 database2_port = database2_name = sym database2_user = root database2_password = test mailer_transport = smtp mailer_host = localhost mailer_user = mailer_password = locale = en secret = ThisTokenIsNotSoSecretChangeIt
二、在app/config/config.yml里配置
# Doctrine Configuration doctrine: dbal: connections: default: driver: %database_driver% host: %database_host% port: %database_port% dbname: %database_name% user: %database_user% password: %database_password% charset: UTF8 another: driver: %database_driver% host: %database2_host% port: %database2_port% dbname: %database2_name% user: %database2_user% password: %database2_password% charset: UTF8 orm: default_entity_manager: default entity_managers: default: connection: default mappings: # AcmeDemoBundle: ~ AcmeTicketBundle: ~ another: connection: another mappings: # AcmeDemoBundle: ~ AcmeTicketBundle: ~ # auto_generate_proxy_classes: %kernel.debug% # auto_mapping: true # Swiftmailer Configuration
三、调用方式
if ($type == 1) { $this->emtitymanager = $this->get('doctrine')->getEntityManager(); //默认 } else { $this->emtitymanager = $this->get('doctrine')->getEntityManager('another'); //其他数据库 }