Use multiple databases in Symfony Projects on Cloudways

by Shahroze Nawaz  August 29, 2017

At the time of writing this article, the most recent version of Symfony is 3.3.6. The community is abuzz with the news of imminent release of Symfony 4.

symfony multiple databases

In many of my previous articles, I have covered Entity Managers and Doctrine in Symfony. The tutorials have covered the process of generating and configuring entities in Symfony. Now a common scenario in real world Symfony projects is the requirement of working with multiple databases. In many cases, the project requires access to two or more databases simultaneously for Doctrine related actions.  I will cover this topic in this tutorial.

For the purpose of this tutorial, I am assuming that you have already installed Symfony on Cloudways and that it’s running properly in the browser.

The Traditional DB Config

While installing Symfony, the Symfony Installer asks questions about the database credentials:

Note:  You can get your Cloudways DB credentials in Application access area of the Cloudways Platform.

The following credentials are available in the parameters.yml file:

Here are the details about the default connection and credentials in the config.yml file:

When you create entities and call the getManager() function, you will get the default connection (that you used while installing Symfony). If you wish to bypass this traditional database setup and wish to use a different or multiple databases in your Symfony projects, you have two options. You could define it in the parameters.yml and then configure it in the config.yml. The second option is to directly define it in config.yml.

Configure Multiple Databases

I will start with the adding parameters to parameters.yml file. The second database connection could be added using the following parameters:

The next step is to get these credentials in the config.yml:

I have added a default connection default_connection: default with no connection specified in the entity manager method. Next, I distributed the configuration of the connections into ‘default’ and ‘database2’.

Finally, I will specify the mapping of each bundle in the project:

At this point, the first option is complete. I will now describe the second method in which I could directly add the database credentials in the config.yml:

The rest of the ORM settings will remain the same.

Now,  I can call the entity manager in the controller by just calling it by name:

The following is the recommended syntax for repository calls, as mentioned in the Symfony docs.Similarly call the name of EM here also.

If a specific entity manager is not called, default will be returned to the controller. To avoid errors, I will add auto_mapping: true to any one of the connection.

Once all this is done, when I am working with the schema and performing Doctrine actions, I will specify the connection name like this:

Final Words

The best thing about Symfony is its very flexible structure that ensures that developers could easily mold the features of the framework to project specifications.

Remember that Cloudways offer one database per Symfony application. If you wish to use the second database, you have to launch the second application and use its DB’s credentials. In this scenario, remember that other configurations in the config.yml should not be affected.

If you have any question or queries, feel free to comment below.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at

Stay Connected:

You Might Also Like...