EntityManagerDecorator's repositories use EntityManager #5123

Open
opened 2026-01-22 14:59:01 +01:00 by admin · 7 comments
Owner

Originally created by @anton-kotik on GitHub (May 11, 2016).

Repositories created by Doctrine\ORM\Decorator\EntityManagerDecorator use EntityManager instead of EntityManagerDecorator:

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Decorator\EntityManagerDecorator;

$entityManager = EntityManager::create($dbParams, $config);
$entityManager->getRepository('entity')->getEntityManager();
// returns: Doctrine\ORM\EntityManager

class TestDecorator extends EntityManagerDecorator 
{
    // ...
}

$decorator = new TestDecorator($entityManager);
$decorator->getRepository('entity')->getEntityManager();
// returns: Doctrine\ORM\EntityManager
// expected: TestDecorator ?

This behavior may be unexpected for custom repositories using decorator object.

Originally created by @anton-kotik on GitHub (May 11, 2016). Repositories created by `Doctrine\ORM\Decorator\EntityManagerDecorator` use `EntityManager` instead of `EntityManagerDecorator`: ``` php use Doctrine\ORM\EntityManager; use Doctrine\ORM\Decorator\EntityManagerDecorator; $entityManager = EntityManager::create($dbParams, $config); $entityManager->getRepository('entity')->getEntityManager(); // returns: Doctrine\ORM\EntityManager class TestDecorator extends EntityManagerDecorator { // ... } $decorator = new TestDecorator($entityManager); $decorator->getRepository('entity')->getEntityManager(); // returns: Doctrine\ORM\EntityManager // expected: TestDecorator ? ``` This behavior may be unexpected for custom repositories using decorator object.
admin added the Improvement label 2026-01-22 14:59:01 +01:00
Author
Owner

@Majkl578 commented on GitHub (Dec 20, 2017):

Hello, I'm afraid this is not something we can fix. Since we're talking about decorator pattern, inner EM instance has no knowledge about being decorated. One possible solution would be using RepositoryFactory that is created for your decorator, not for inner EM by default.

@Majkl578 commented on GitHub (Dec 20, 2017): Hello, I'm afraid this is not something we can fix. Since we're talking about decorator pattern, inner EM instance has no knowledge about being decorated. One possible solution would be using RepositoryFactory that is created for your decorator, not for inner EM by default.
Author
Owner

@Isinlor commented on GitHub (Mar 28, 2018):

Just to give people reading this a well visible notice.

There is a similar issue to this one with transactional method: https://github.com/doctrine/doctrine2/issues/7161

@Isinlor commented on GitHub (Mar 28, 2018): Just to give people reading this a well visible notice. There is a similar issue to this one with `transactional` method: https://github.com/doctrine/doctrine2/issues/7161
Author
Owner

@stof commented on GitHub (Apr 27, 2018):

The difference is that #7161 can workaround it, while this one cannot

@stof commented on GitHub (Apr 27, 2018): The difference is that #7161 can workaround it, while this one cannot
Author
Owner

@stof commented on GitHub (Apr 27, 2018):

I think event objects will also have the inner EM (as well as any place receiving the EM from the EM itself as $this)

@stof commented on GitHub (Apr 27, 2018): I think event objects will also have the inner EM (as well as any place receiving the EM from the EM itself as `$this`)
Author
Owner

@Isinlor commented on GitHub (Apr 27, 2018):

Actually it does have a workaround:

class CustomDecorator extends EntityManagerDecorator {

    protected $repositoryFactory;

    // In Symfony 2.* $config can be injected with @doctrine.orm.default_configuration
    public function __construct(EntityManagerInterface $wrapped, Configuration $config)
    {
        $this->repositoryFactory = $config->getRepositoryFactory();
        parent::__construct($wrapped);
    }


    /**
     * Gets the repository for an entity class.
     *
     * Fixes: https://github.com/doctrine/doctrine2/issues/5820
     *
     * @param string $entityName The name of the entity.
     *
     * @return \Doctrine\ORM\EntityRepository The repository class.
     */
    public function getRepository($entityName)
    {
        return $this->repositoryFactory->getRepository($this, $entityName);
    }

}
@Isinlor commented on GitHub (Apr 27, 2018): Actually it does have a workaround: ```php class CustomDecorator extends EntityManagerDecorator { protected $repositoryFactory; // In Symfony 2.* $config can be injected with @doctrine.orm.default_configuration public function __construct(EntityManagerInterface $wrapped, Configuration $config) { $this->repositoryFactory = $config->getRepositoryFactory(); parent::__construct($wrapped); } /** * Gets the repository for an entity class. * * Fixes: https://github.com/doctrine/doctrine2/issues/5820 * * @param string $entityName The name of the entity. * * @return \Doctrine\ORM\EntityRepository The repository class. */ public function getRepository($entityName) { return $this->repositoryFactory->getRepository($this, $entityName); } }
Author
Owner

@githoober commented on GitHub (May 22, 2018):

And then you find that SQLBuilder also has a wrapped entity manager instance, you fix that, only to find that all collections also have the wrapped instance embedded. In the end, there is no point in using a decorated entity manager because of that because it's impossible to make it work.

@githoober commented on GitHub (May 22, 2018): And then you find that SQLBuilder also has a wrapped entity manager instance, you fix that, only to find that all collections also have the wrapped instance embedded. In the end, there is no point in using a decorated entity manager because of that because it's impossible to make it work.
Author
Owner

@andrews05 commented on GitHub (Sep 17, 2019):

Any workaround for getting the decorator in lifecycle callbacks?

@andrews05 commented on GitHub (Sep 17, 2019): Any workaround for getting the decorator in lifecycle callbacks?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5123