You will have to configure the two connections in your file config.yml
doctrine:
dbal:
default_connection: default
connections:
mysql:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
sqlserver:
driver: pdo_sqlsrv
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
orm:
default_entity_manager: mysql
entity_managers:
mysql:
connection: mysql
mappings:
AppBundle: ~
AcmeStoreBundle: ~
sqlserver:
connection: sqlserver
mappings:
AcmeCustomerBundle: ~
VERY IMPORTANT It is necessary that in the field mappings
indicate which entities belong to each connection.
In the controller, or service, when you are going to use the EntityManager
, you will have to indicate which connection to use. For example.
<?php
// dentro del controlador
$emMySQL = $this->getDoctrine()->getManager('mysql');
$emSQLServer = $this->getDoctrine()->getManager('sqlserver');
In the case of mysql
, you could skip passing the name of the connection, since we would have configured it as the default connection here:
...
orm:
default_entity_manager: mysql
...
UPDATE
If all your entities are in the same bundle, you will have to configure it in the following way:
mysql:
mappings:
AppBundle:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/MySQL"
#the prefix
prefix: "Your\Bundle\Entity\MySQL"
sqlserver:
mappings:
AppBundle:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/SQLServer"
#the prefix
prefix: "Your\Bundle\Entity\SQLServer"
You may have to alter the directory structure / namespaces you have right now.