Skip to content

Commit de3b138

Browse files
committed
minor #14336 [Doctrine] Add caution about a single entity being managed by multiple managers (Guillaume)
This PR was merged into the 4.4 branch. Discussion ---------- [Doctrine] Add caution about a single entity being managed by multiple managers Fixes #9878 This continues the great work done in #12320 by @ipernet . Commits ------- de39e50 Multiple entity managers and shared entities management
2 parents 972faae + de39e50 commit de3b138

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

@@ -283,4 +284,26 @@ The same applies to repository calls::
283284
}
284285
}
285286

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

0 commit comments

Comments
 (0)