Skip to content

[DoctrineBridge] Fix resetting the manager when using native lazy objects #60535

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
May 25, 2025

Conversation

HypeMC
Copy link
Member

@HypeMC HypeMC commented May 25, 2025

Q A
Branch? 7.3
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

PR #59913 doesn't seem to work as expected. For example, a simple test like this:

class DoctrineTest extends KernelTestCase
{
    public function testManagerRegistryReset(): void
    {
        $entityManager = self::getContainer()->get(EntityManagerInterface::class);
        $entityManager->close();

        self::assertFalse($entityManager->isOpen());

        $managerRegistry = self::getContainer()->get(ManagerRegistry::class);
        $managerRegistry->resetManager();

        self::assertTrue($entityManager->isOpen());
    }
}

fails with:

There was 1 error:

  1. App\Tests\DoctrineTest::testManagerRegistryReset
    Error: Call to a member function __construct() on false

/project/var/cache/test/ContainerTVZSKa5/App_KernelTestDebugContainer.php:709
/project/vendor/symfony/doctrine-bridge/ManagerRegistry.php:91
/project/vendor/doctrine/orm/src/EntityManager.php:534
/project/tests/DoctrineTest.php:21

Here's the dumped container for reference:

protected static function getDoctrine_Orm_DefaultEntityManagerService($container, $lazyLoad = true)
{
    if (true === $lazyLoad) {
        return $container->services['doctrine.orm.default_entity_manager'] = new \ReflectionClass('Doctrine\ORM\EntityManager')->newLazyGhost(static function ($proxy) use ($container) { self::getDoctrine_Orm_DefaultEntityManagerService($container, $proxy); });
    }

    include_once \dirname(__DIR__, 4).'/vendor/doctrine/orm/src/Proxy/Autoloader.php';
    include_once \dirname(__DIR__, 4).'/vendor/doctrine/persistence/src/Persistence/ObjectManager.php';
    include_once \dirname(__DIR__, 4).'/vendor/doctrine/orm/src/EntityManagerInterface.php';
    include_once \dirname(__DIR__, 4).'/vendor/doctrine/orm/src/EntityManager.php';

    $instance = ($lazyLoad->__construct(($container->services['doctrine.dbal.default_connection'] ?? self::getDoctrine_Dbal_DefaultConnectionService($container)), ($container->privates['doctrine.orm.default_configuration'] ?? self::getDoctrine_Orm_DefaultConfigurationService($container)), ($container->privates['doctrine.dbal.default_connection.event_manager'] ?? self::getDoctrine_Dbal_DefaultConnection_EventManagerService($container))) && false ?: $lazyLoad);

    ($container->privates['doctrine.orm.default_manager_configurator'] ??= new \Doctrine\Bundle\DoctrineBundle\ManagerConfigurator([], []))->configure($instance);

    return $instance;
}

cc @nicolas-grekas

@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit e2e15f9 into symfony:7.3 May 25, 2025
10 of 11 checks passed
@HypeMC HypeMC deleted the fix-lazy-em branch May 25, 2025 10:37
@fabpot fabpot mentioned this pull request May 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants