8

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?

Community
  • 1
  • 1
Capitaine
  • 1,923
  • 6
  • 27
  • 46

1 Answers1

6

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.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • thanks for the reply. Still one problem not yet solved. As I have some same table names in different databases. So it's better to organised them into separte folders inside Entity Folder. I have checked http://stackoverflow.com/questions/13765785/doctrine-generate-entities-namespace-issue but it seems it cannot not be done right? Are there workarounds for this problem – Capitaine Mar 28 '13 at 09:35
  • The only direct work around is to add the database name to the table name. But doing so means the various doctrine commands will stop working and it means hard coding database names. Depending on your needs, it's possible that you might be able to use sql views to basically change the table names. I think you will be better off just using multiple bundles. – Cerad Mar 28 '13 at 12:21
  • 1
    You should create a new symfony install, generate a bundle pull down your tables and then edit the namespaces and copy the files over into your existing bundle. It's a pain, but its easier than trying to hack your way around in the mapping generator. You can definitely have namespaces under entity (I have several levels in my projects). `Bundle/Entity/ThingsThatMakeMeMad/SpilledMilk.php` etc. – james_t Apr 02 '13 at 14:18
  • thanks for the tips. Well, if too many manual operations are involved for syncing doctrine objects, then I would prefer using the simple php mysql connect command to query databases instead. – Capitaine Apr 04 '13 at 08:23