doctrine/DoctrineBundle

Doctrine listeners not referencing the right services

Closed this issue · 5 comments

Hi,

Environement test
Symfony 4.4.8
Doctrine 2.0.8

Description
Doctrine subscribers (or listeners) services are not the same if fetched from container or fetched by doctrine events. If i dump the spl_object_id of a doctrine subscriber fetched from the container and the spl_object_id of the same doctrine subscriber but fired by a doctrine event (like onFlush), this is not the same id.
It implies that all injected services are also not the right ones (are they duplicated ?). Messenger transports are also impacted : transport is duplicated when coming from doctrine event

Maybe the test container is guilty ?

Symfony issue : symfony/symfony#36783

I wasn't able to reproduce this with

<?php
namespace App\EventSubscriber;

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventManager;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Psr\Container\ContainerInterface;

class Issue36783Subscriber implements EventSubscriber
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function postLoad(EventArgs $eventArgs)
    {
        var_dump($this->container->get(self::class) === $this);
    }

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

Please post failing test case, or reproducer according instructions I posted in linked issue.

Codeception symfony module reboots kernel by default in https://github.com/Codeception/module-symfony/blob/e60d4266644099ba2cccad9ae22cef3ceb6c0039/src/Codeception/Module/Symfony.php#L214 so you probably want to use rebootable_client option. Nothing to do with Doctrine, please raise your concerns at https://github.com/Codeception/module-symfony if you still have any

Actually using that option does not help, however this is still an issue of Symfony module, as commenting out rebootKernel call from \Codeception\Lib\Connector\Symfony::__construct causes your expected behaviour

Not using Doctrine2 module make it works (see Codeception/Codeception#3497)

It is a solution for us as we only use Db module