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

Listener object (doctrine.orm.entity_listener tagged) have other copy of EntityManager (injected by autowiring) #28869

Open
makm opened this issue Oct 14, 2018 · 17 comments

Comments

@makm
Copy link

makm commented Oct 14, 2018

Hello. I have a problem with two diffents copy of em

<?php
namespace App\Listener;
use Doctrine\ORM\EntityManagerInterface;

class FirstListener
{
    public function __construct(EntityManagerInterface $entityManager)
    {
        dump('create FirstListener');
        dump($entityManager);
    }
}
<?php
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number(EntityManagerInterface $entityManager)
    {
        dump('number action invoke');
        dump($entityManager);

        return new Response(
            '<html><body>hello</body></html>'
        );
    }
}

service.yaml:

    App\Listener\FirstListener:
      tags:
      - { name: doctrine.orm.entity_listener }

result:
image

why ?
for investigation I use:
composer create-project symfony/skeleton blog

@makm makm changed the title Listener object (doctrine.orm.entity_listener tagger) have other copy of EntityManager (injected by autowiring) Listener object (doctrine.orm.entity_listener tagged) have other copy of EntityManager (injected by autowiring) Oct 14, 2018
@xabbuh
Copy link
Member

xabbuh commented Oct 15, 2018

Can you create a small example application that allows to reproduce your issue?

@makm
Copy link
Author

makm commented Oct 15, 2018

very simple. Based on symfony skeleton
https://github.com/makm/em-dupe-bug

@dmaicher
Copy link
Contributor

dmaicher commented Oct 15, 2018

Hmm I think this case should result in a ServiceCircularReferenceException?

If you mark your FirstListener service as lazy: true + require symfony/proxy-manager-bridge it should behave correctly in the meantime.

@makm
Copy link
Author

makm commented Oct 15, 2018

Nope. You can see service.yaml (first post) Very simple.
I used xdebug for investigation this problem: symfony create second container

@dmaicher
Copy link
Contributor

Nope.

yes currently it does not throw any ServiceCircularReferenceException 😊 But I think it should. Because while building the entity manager service it creates your listener service (which itself needs the entity manager).

@makm
Copy link
Author

makm commented Oct 17, 2018

I think there is no 'Circular Reference'. Simple injection for listener as service.
Second: It's problem actual only for Entity Manager.

@dmaicher
Copy link
Contributor

I think there is no 'Circular Reference'. Simple injection for listener as service.

For me this is a circular reference.

EntityManager -> EntityListenerResolver -> FirstListener -> EntityManager

Waiting for @nicolas-grekas to have a look and confirm

@makm
Copy link
Author

makm commented Oct 17, 2018

In my case: I use service which use EntityManager.
Thats mean - I can't use any services with EntityManager included.

@dmaicher
Copy link
Contributor

Thats mean - I can't use any services with EntityManager included.

That's not correct. Your service is a bit special because its tagged as doctrine.orm.entity_listener.

@fernetmatt
Copy link

fernetmatt commented Nov 5, 2018

I've experienced the same issue using the factory method:

@doctrine.orm.default_entity_manager::getRepository($entityName)

To solve this i had to switch to @doctrine.orm.container_repository_factory::getRepository($EntityManager, $EntityName)

repository.user:
    class: Infrastructure\Persistence\DoctrineORM\Repository\UserRepository
    factory: ["@doctrine.orm.container_repository_factory", getRepository]
    arguments:
      - '@doctrine.orm.default_entity_manager'
      - 'Domain\Entity\User'

Refer to: stackoverflow question

@Padam87
Copy link
Contributor

Padam87 commented Feb 7, 2019

I have the same issue.

I have 2 services, each getting a different instance of the manager. (spl_object_hash different) Both are using simple constructor injection, autowired, and type hinted as Doctrine\ORM\EntityManagerInterface.

These manager have different units of work, so have different managed entities.

@ro0NL
Copy link
Contributor

ro0NL commented Feb 7, 2019

Is this a duplicate of #30091 also? If so, does #30096 solve it?

@Padam87
Copy link
Contributor

Padam87 commented Feb 7, 2019

@ro0NL Thank you, this fixed my issue.

The original problem in this issue seem a bit older, so it should be different as @nicolas-grekas pointed out in the PR.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Hello? This issue is about to be closed if nobody replies.

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

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

7 participants