Skip to content

Commit 385a239

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Multiple entity managers and shared entities management
2 parents 2d3dbd6 + 16550e7 commit 385a239

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

doctrine/multiple_entity_managers.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ application. This is necessary if you are using different databases or even
99
vendors with entirely different sets of entities. In other words, one entity
1010
manager that connects to one database will handle some entities while another
1111
entity manager that connects to another database might handle the rest.
12+
It is also possible to use multiple entity managers to manage a common set of
13+
entities, each with their own database connection strings or separate cache configuration.
1214

1315
.. note::
1416

@@ -44,7 +46,6 @@ The following configuration code shows how you can configure two entity managers
4446
driver: 'pdo_mysql'
4547
server_version: '5.7'
4648
charset: utf8mb4
47-
4849
orm:
4950
default_entity_manager: default
5051
entity_managers:
@@ -183,7 +184,7 @@ In this case, you've defined two entity managers and called them ``default``
183184
and ``customer``. The ``default`` entity manager manages entities in the
184185
``src/Entity/Main`` directory, while the ``customer`` entity manager manages
185186
entities in ``src/Entity/Customer``. You've also defined two connections, one
186-
for each entity manager.
187+
for each entity manager, but you are free to define the same connection for both.
187188

188189
.. caution::
189190

@@ -290,4 +291,26 @@ The same applies to repository calls::
290291
}
291292
}
292293

294+
.. caution::
295+
296+
One entity can be managed by more than one entity manager. This however
297+
results in unexpected behavior when extending from ``ServiceEntityRepository``
298+
in your custom repository. The ``ServiceEntityRepository`` always
299+
uses the configured entity manager for that entity.
300+
301+
In order to fix this situation, extend ``EntityRepository`` instead and
302+
no longer rely on autowiring::
303+
304+
// src/Repository/CustomerRepository.php
305+
namespace App\Repository;
306+
307+
use Doctrine\ORM\EntityRepository;
308+
309+
class CustomerRepository extends EntityRepository
310+
{
311+
// ...
312+
}
313+
314+
You should now always fetch this repository using ``ManagerRegistry::getRepository()``.
315+
293316
.. _`several alternatives`: https://stackoverflow.com/a/11494543

0 commit comments

Comments
 (0)