symfony/symfony — Issue #2594: Change the default EntityManager at runtime
Author: sliver
Created: 2011-11-10 07:03:52+00:00
State: open

Hi,

I am working to set up MySQL replication in my Symfony2 project.

I believe separating only-read or read-write actions in controller is simple and less side effects. Even, we can do cache things more easily when we know which action is read-only.

Here is my thought:
1. Setup 2 EntityManager for master and slave, and the default will be the master one. This is pretty easy and works great.
2. Recognize which action is read-only. We can recognize it by the http method from request or just set it manually.
3. Change the default EntityManager to master or slave at the beginning of every action. This is the problem. I don't know how to do this actually.

The Symfony\Bundle\DoctrineBundle\Registry class can getDefaultEntityManagerName(),
but there is lack of setDefaultEntityManagerName method.

I think we can add the set method to change the default EntityManager at runtime.