-2

Using Symfony 4 or 5, im trying to have a single app that talks to doctrine through the entity manager transparently using the default EM but changing connection based on locale.

The idea is to some how check the locale on run time and decide for the correct db connection to use for the entire request. That is, $doctrine->getEntityManager() should return a manager with the correct connection both for use on the repositories but also by the firewall handling auth and any other service that connects to DB.

BONUS should be possible when running commands to specify what connection to use.

Details:
- Symfony 4 or greater.
- PHP 7>.
- The same model is used for every country.
- For http requests the locale is in the url.
- The url to all databases are in the env files

My own attempt at this:

If tried extending Doctrine\DBAL\Connection to make a wrapper using doctrines config but have failed to find a way to change all connection settings. (change the URL to master and slave DBs) For both requests and console.

Any help is greatly appreciated

nachocifu
  • 100
  • 4

1 Answers1

0

Its not a direct solution for your problem, but could be helpful.

When i wanted to have different database connection with the same entities and model, i could have used multiple connection, but this was more stressful than anything else. I could not include automatic testing and debugging was horrific.

My solution last year was to use the same code but with different config:

  • one code base stashed on git or private repository
  • different config files for each database connection

So now we have separated the logic for handling the database connection and we cannot reuse the entities and models.

After this i started to use docker. So i created 1 image with php:7.4-fpm. Now i could start this container with the default config provided in my git repository.

If i want to change the database connection i just mount an external .env.local into the image and start that as a container.

Now i have to container, one with default config, one with specific database connection. Both are running in parallel and can be automatic tested with their different database connection.

Hope this helps you.

CasualBen
  • 829
  • 8
  • 22