orm icon indicating copy to clipboard operation
orm copied to clipboard

[QUESTION] Singleton entity manager in DoctrineServiceProvider.

Open pichanz opened this issue 5 years ago • 2 comments

Package version 1.5.5, Laravel version 6.17.1

Hi i got a question about the DoctrineServiceProvider specifically about singleton binding for 'em'. Here's a snippet of the DoctrineServiceProvider.

    /**
     * Setup the entity manager
     */
    protected function registerEntityManager()
    {
        // Bind the default Entity Manager
        $this->app->singleton('em', function ($app) {
            return $app->make('registry')->getManager();
        });

        $this->app->alias('em', EntityManager::class);
        $this->app->alias('em', EntityManagerInterface::class);
    }

    /**
     * Register the manager registry
     */
    protected function registerManagerRegistry()
    {
        $this->app->singleton('registry', function ($app) {
            $registry = new IlluminateRegistry($app, $app->make(EntityManagerFactory::class));

            // Add all managers into the registry
            foreach ($app->make('config')->get('doctrine.managers', []) as $manager => $settings) {
                $registry->addManager($manager, $settings);
            }

            return $registry;
        });

I get that we need it to be a singleton instance for em. But when i want to reset the entity manager via the registry (IlluminateRegistry::resetManager())

The reset entity manager is not rebound because of the singleton binding of 'em'. So since the IlluminateRegistry is already singleton, i think changing the singleton binding to non singleton should be applicable.

I want to ask if this is intentional to bind the 'em' as the singleton because when i want to reset the manager i don't want to rebind the bound 'em' in the Container.

Thank you

pichanz avatar Jul 01 '20 12:07 pichanz

You are right. If the default EntityManager is reset, should we also rebind it, I think.

There is more problems with resetting an EntityManager: #398

eigan avatar Jul 02 '20 06:07 eigan

Hey eigan, thank you for answering!

Now that you mention problem #398, i think rebinding the em would not solve the problem but will just cause further problems in the application state. For example, if i got a customized event subscriber on the EntityManager, i think all the subscribed event in the em will be gone to, as it is a completely new instance.

I think it'd just be better to just restart the application on closed em, rather than resetting the manager.

pichanz avatar Jul 02 '20 08:07 pichanz