@@ -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 :
@@ -155,23 +156,23 @@ The following configuration code shows how you can configure two entity managers
155
156
'connection' => 'default',
156
157
'mappings' => [
157
158
'Main' => [
158
- ' is_bundle' => false,
159
- ' type' => 'annotation',
160
- ' dir' => '%kernel.project_dir%/src/Entity/Main',
161
- ' prefix' => 'App\Entity\Main',
162
- ' alias' => 'Main',
159
+ is_bundle => false,
160
+ type => 'annotation',
161
+ dir => '%kernel.project_dir%/src/Entity/Main',
162
+ prefix => 'App\Entity\Main',
163
+ alias => 'Main',
163
164
]
164
165
],
165
166
],
166
167
'customer' => [
167
168
'connection' => 'customer',
168
169
'mappings' => [
169
170
'Customer' => [
170
- ' is_bundle' => false,
171
- ' type' => 'annotation',
172
- ' dir' => '%kernel.project_dir%/src/Entity/Customer',
173
- ' prefix' => 'App\Entity\Customer',
174
- ' alias' => 'Customer',
171
+ is_bundle => false,
172
+ type => 'annotation',
173
+ dir => '%kernel.project_dir%/src/Entity/Customer',
174
+ prefix => 'App\Entity\Customer',
175
+ alias => 'Customer',
175
176
]
176
177
],
177
178
],
@@ -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
@@ -283,4 +284,26 @@ The same applies to repository calls::
283
284
}
284
285
}
285
286
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/HumanRepository.php
298
+ namespace App\Repository;
299
+
300
+ use Doctrine\ORM\EntityRepository;
301
+
302
+ class HumanRepository extends EntityRepository
303
+ {
304
+ // ...
305
+ }
306
+
307
+ You should now always fetch this repository using ``ManagerRegistry::getRepository() ``.
308
+
286
309
.. _`several alternatives` : https://stackoverflow.com/a/11494543
0 commit comments