Skip to content
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

DI: different instances of the same service during a single execution? #30465

Closed
fabienlem opened this issue Mar 6, 2019 · 3 comments
Closed

Comments

@fabienlem
Copy link

fabienlem commented Mar 6, 2019

Symfony version(s) affected: 3.4.22

Description
I found out that it is possible to get different instances of the same service (eg: entity manager) during a single execution.

How to reproduce

class MyDoctrineSubscriber implements \Doctrine\Common\EventSubscriber
{
    protected $myService;

    public function __construct(MyService $myService)
    {
        $this->myService = $myService;
    }

    public function preFlush(PreFlushEventArgs $args)
    {
        // an instance of the entity manager
        $entityManager = $args->getEntityManager();

        $this->myService->doSomething();
    }

    public function getSubscribedEvents()
    {
        return [
            Events::preFlush,
        ];
    }
}


class MyService
{
    protected $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        // another instance of the same entity manager (!?)
        $this->entityManager = $entityManager;
    }
	
    public function doSomething()
    {
        $myEntity = new MyEntity();

        // entity is not persisted as persist and flush are called on different instances
        $this->entityManager->persist($myEntity); 
    }
}

Is that an expected behavior?

@xabbuh
Copy link
Member

xabbuh commented Mar 6, 2019

This looks like the same as #30091.

@stof
Copy link
Member

stof commented Mar 6, 2019

your service probably gets the proxy object (DoctrineBundle defines the EntityManager as a lazy service, to be able to replace the actual instance if we need to reset the entity manager, as that feature is not available natively in Doctrine ORM) while the event object contains the actual instance directly (as it gets instantiated by the actual instance, passing $this).
That was always the case, even without the bug.

@nicolas-grekas
Copy link
Member

This should be fixed. Please report back if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants