Multiple dynamic EntityManagers in Symfony2

Symfony2 is fantastic when coupled with Doctrine, but unfortunately Doctrine has its limitations and does not inherently support dynamic EntityManager connections, because the parameters for each connection must be defined and loaded at the kernel level.

This assumes that each of the connections have an identical schema mapped by Doctrine2.

My live implementation functions for a single/multi tenant shared architecture in which connection parameters are encrypted and housed in MasterDatabase, while ChildDatabase1, ChildDatabase2, all have the exact same schema, but isolated data for a variety of business reasons.

There are several components to making this work:

  1. app/config/parameters.yml
  2. app/config/config.yml
  3. app/config/services.yml
  4. MyApp/Services/ApplicationConnector.php

app/config/parameters.yml

app/config/config.yml

app/config/services.yml

MyApp/Services/ApplicationConnector.php

Usage in a Controller

 

8 Replies to “Multiple dynamic EntityManagers in Symfony2”

    1. I wouldn’t recommend having 10 simultaneous connections, but if you needed that in your use case, you’d want to make sure they were all uniquely named, and then a schema update would require some modifications to doctrine-migrations (assuming you’re using Doctrine). You can do this with the –em parameter in your migrations command(s).

  1. Great bit of code! Thanks a lot! Could you help with one teeny-tiny thing tho, please? Apparently resetting the entity manager is now deprecated in Symfony 3.2 (does work but will stop once they release 4.0), do you have any idea on how to refactor that line to be future-proof? Thanks!

  2. Hello jake
    First of all apologies for my English. (It is a translation of Spanish and I do not know how it will come out).
    The solution you raise in your article is very well explained and has worked perfectly. It was a great help because she was very lost.
    I have the same application that uses several client databases. I have the default connection that contains the bd with the connection data of each bd of the clients and the dynamic connection that I am assigning the bd of the client. This I have already been able to configure with your code.

    Now I have passed the following problem: the Fosuserbundle configuration.
    Each bd of my clients have their own table of users independent of the others. But I do not know how I can do so that the login of the application is made against the taba users of the bd of the selected client.

    I tried to modify the loginAction of the SecurityControler driver whose parent is FOSUserBundle but I did not get it to work.

    Do you know how I have to do it?
    Thanks in advance.

    Greetings,

    Inma.

    1. I try to avoid FOSUserBundle because it does not allow the type of wild customizations I typically need to accommodate things easily for my clients. Can you be a little more specific about what isn’t working and maybe I can help; if not I know the FOS crew is fantastic and I’m sure has plenty of great resources available.

Leave a Reply

Your email address will not be published. Required fields are marked *