Skip to content

[Doctrine] Add caution about a single entity being managed by multiple managers #14336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions doctrine/multiple_entity_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ application. This is necessary if you are using different databases or even
vendors with entirely different sets of entities. In other words, one entity
manager that connects to one database will handle some entities while another
entity manager that connects to another database might handle the rest.
It is also possible to use multiple entity managers to manage a common set of
entities, each with their own database connection strings or separate cache configuration.

.. note::

Expand Down Expand Up @@ -44,7 +46,6 @@ The following configuration code shows how you can configure two entity managers
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4

orm:
default_entity_manager: default
entity_managers:
Expand Down Expand Up @@ -183,7 +184,7 @@ In this case, you've defined two entity managers and called them ``default``
and ``customer``. The ``default`` entity manager manages entities in the
``src/Entity/Main`` directory, while the ``customer`` entity manager manages
entities in ``src/Entity/Customer``. You've also defined two connections, one
for each entity manager.
for each entity manager, but you are free to define the same connection for both.

.. caution::

Expand Down Expand Up @@ -283,4 +284,26 @@ The same applies to repository calls::
}
}

.. caution::

One entity can be managed by more than one entity manager. This however
result in unexpected behavior when extending from ``ServiceEntityRepository``
in your custom repository. The ``ServiceEntityRepository`` always
uses the configured entity manager for that entity.

In order to fix this situation, extend ``EntityRepository`` instead and
no longer rely on autowiring::

// src/Repository/CustomerRepository.php
namespace App\Repository;

use Doctrine\ORM\EntityRepository;

class CustomerRepository extends EntityRepository
{
// ...
}

You should now always fetch this repository using ``ManagerRegistry::getRepository()``.

.. _`several alternatives`: https://stackoverflow.com/a/11494543