@@ -9,6 +9,8 @@ application. This is necessary if you are using different databases or even
9
9
vendors with entirely different sets of entities. In other words, one entity
10
10
manager that connects to one database will handle some entities while another
11
11
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.
12
14
13
15
.. note ::
14
16
@@ -44,7 +46,6 @@ The following configuration code shows how you can configure two entity managers
44
46
driver : ' pdo_mysql'
45
47
server_version : ' 5.7'
46
48
charset : utf8mb4
47
-
48
49
orm :
49
50
default_entity_manager : default
50
51
entity_managers :
@@ -183,7 +184,7 @@ In this case, you've defined two entity managers and called them ``default``
183
184
and ``customer ``. The ``default `` entity manager manages entities in the
184
185
``src/Entity/Main `` directory, while the ``customer `` entity manager manages
185
186
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 .
187
188
188
189
.. caution ::
189
190
@@ -290,4 +291,26 @@ The same applies to repository calls::
290
291
}
291
292
}
292
293
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
+
293
316
.. _`several alternatives` : https://stackoverflow.com/a/11494543
0 commit comments