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

 

Categories: Random

8 Comments

Jehan · October 21, 2015 at 2:56 am

Thanks a lot! It’s really a great help to me !

Sudin · November 25, 2015 at 6:51 am

If there are 10 different database connected with dynamic connection. What is the solution for schema update ?

    Jake Litwicki · November 25, 2015 at 9:49 am

    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).

beez · January 28, 2017 at 6:14 pm

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!

    Jake Litwicki · February 7, 2017 at 10:15 am

    That’s a good question; where did you find that? I’ll take a look and see what we can do!

Inma Ruiz · March 14, 2017 at 2:22 am

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.

    Jake Litwicki · March 23, 2017 at 9:30 pm

    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.

      Inma Ruiz · March 24, 2017 at 1:46 am

      Thanks Jake, I’ll see if the FOS team clarifies something.
      Greetings,
      Inma.

Leave a Reply

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