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

[QUESTION] Singleton entity manager in DoctrineServiceProvider. #448

Open
pichanz opened this issue Jul 1, 2020 · 2 comments
Open

[QUESTION] Singleton entity manager in DoctrineServiceProvider. #448

pichanz opened this issue Jul 1, 2020 · 2 comments

Comments

@pichanz
Copy link

pichanz commented Jul 1, 2020

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

@eigan
Copy link
Member

eigan commented Jul 2, 2020

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

@pichanz
Copy link
Author

pichanz commented Jul 2, 2020

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.

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

No branches or pull requests

2 participants