Too few arguments to function `LoginFormAuthenticator::__construct()`, 0 passed exactly 4 expected

As already said, injecting EntityManager will get you the default one. Doctrine will register one service per manager with the name doctrine.orm.%manager_name%_entity_manager.

So if you need a different one, you have several options:

1. Explicitly configure your service to use the manager you need:

services:
    App\Security\LoginFormAuthenticator: 
        arguments:
            # Override the Manager, other arguments will be autowired.
            $entityManager: '@doctrine.orm.lc_users_entity_manager'

2. Create different bindings for the different entity managers:

_defauts:
    bind:
        $cvoManager: '@doctrine.orm.lc_cvo_entity_manager'
        $usersManager: '@doctrine.orm.lc_users_entity_manager'
        # Other managers...

Based on the variable name on your dependency, the appropiate manager will be injected:

public function __construct(
    EntityManagerInterface $entityManager, // Default manager
    EntityManagerInterface $usersManager,
)

3. If you have many dependencies, you can also inject ManagerRegistry (the object you get in an AbstractController when you call getDoctrine()). It acts as a service locator from where you can retrieve managers or repositories:

    use Doctrine\Persistence\ManagerRegistry;

    public function __construct(ManagerRegistry $registry) {
        $userManager = $registry->getManager('users');
    }

The first option can get unwieldy fast if you have many services with different dependencies, and the third one will mask your real dependencies and complicate testing if you need to mock the registry.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top