Home » Php » php – Symfony2: How to generate Entities from MULTIPLE Existing Databases in SAME Bundle?

php – Symfony2: How to generate Entities from MULTIPLE Existing Databases in SAME Bundle?

Posted by: admin July 12, 2020 Leave a comment

Questions:

My goal is to get access to multiple databases in One Project Bundle.

I read through the symfony2 docs and managed to do the followings:

  1. configure multiple connections for different Bundles
  2. generate Entities from ONE Existing Database using:

    php app/console doctrine:mapping:import AcmeBlogBundle annotation
    php app/console doctrine:generate:entities AcmeBlogBundle
    

But I cannot find ways to generate Entities from MULTIPLE Existing Databases in SAME Bundle so that I can access multiple databases in One Bundle. Any Ideas?

P.S. I am not familiar with Doctrine. So actually if there are ways to do Symfony2 without Doctrine, I would also appreciate.

UPDATE #1:

Cerad’s answer comes quite close. Yet one problem is not yet solved. As I have some same table names in different databases, it’s better to organised them into separte folders inside Entity Folder. I have checked similar posts like this and that. But the solutions are not working for me. Their solution simply puts all entities directly into Entity Folder, ignoring the specified dir option in config.yml. Are there workarounds for this problem?

How to&Answers:

The first step it to configure multiple entity managers (not connections), one for each database. You then use the –em option on the doctrine commands to specify which entity manager to use.

php app/console doctrine:mapping:import "AcmeBlogBundle" annotation --em=name1
php app/console doctrine:mapping:import "AcmeBlogBundle" annotation --em=name2

Be aware that you not going to be able to directly query (join) across multiple database with doctrine. At least not very easily. As long as you plan on limiting your queries to one database at a time then you will be fine.

This is actually a somewhat advanced topic. You might want to spend some time with the doctrine documentation. Might also be easier to get started with one database and then split later.