Symfony Allows you to connect multiple databases. You need to do a bt workout to achieve this.
Step#1
Add parameters to parameters.yml
file. The second database connection could be added using the following parameters:
Parameters:
# First database
database_host: 127.0.0.1
database_port: null
database_name: qmsfumgabd
database_user: qmsfumgabd
database_password: xxx9bxxMxx
# Second database
database2_host: 127.0.0.1
database2_port: null
database2_name: huscqxzwaw
database2_user: huscqxzwaw
database2_password: dxxxFXxxxB
Step#2
The next step is to get these credentials in the config.yml
:
doctrine:
# Configure the abstraction layer
dbal:
# Set the default connection to default
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
database2:
driver: pdo_mysql
host: '%database2_host%'
port: '%database2_port%'
dbname: '%database2_name%'
user: '%database2_user%'
password: '%database2_password%'
charset: UTF8
Step#3
Finally, specify the mapping of each bundle in the project:
# Configure the ORM
orm:
default_entity_manager: default
entity_managers:
# Register which bundle should use which connection
default:
connection: default
mappings:
AppBundle: ~
DemoBundle: ~
database2:
connection: database2
mappings:
CloudwaysBundle: ~
Step#4
Now for calling any of the entity manager use connection names simply:
class UserController extends Controller
{
public function indexAction()
{
// All 3 return the "default" entity manager
$em = $this->getDoctrine()->getManager();
$em = $this->getDoctrine()->getManager('default');
$em = $this->get('doctrine.orm.default_entity_manager');
// Both of these return the "database2" entity manager
$anotherEm = $this->getDoctrine()->getManager('database2');
$anotherEm = $this->get('doctrine.orm.database2_entity_manager');
}
}
You can also take help from symfony's documentation for multiple databases.