3

In my Symfony 4 project I've created a HelloBlock class in the /src/Blocks/Hello/HelloBlock.php file.

Here's its constructor...

public function __construct(EntityManagerInterface $entityManager)
{
    $this->em = $entityManager;
}

And then in my services.yaml I've added this...

    App\Blocks\:
        resource: '../src/Blocks'
        tags: ['controller.service_arguments']

When running my code (dev environment, cache cleared, etc) I'm getting the "Too few arguments" error. It's not injecting the dependency.

Can anyone help? I thought this is what the Symfony DI is supposed to do.

Thanks!

Michael
  • 103
  • 8

1 Answers1

1

Missing arguments:

You probably have to provide the arguments: to the service definition.

App\Blocks\:
    resource: '../src/Blocks'
    tags: ['controller.service_arguments']
    arguments:
        - '@doctrine.orm.default_entity_manager'

@ is used to not interpret the name as a simple string but to fetch the actual service instead.

Default Doctrine naming:

The naming is a little bit tricky; this answer made me understand how the naming is built: <entitymanager_name> (according to the doctrine.orm.entity_managers YAML definition) concatenated to _entity_manager.

With the special case of the default one that is available as doctrine.orm.default_entity_manager even when not explicitly defined in the above mentioned config key.

My assumption:

I tried on my app to just add that string as an argument and it didn't fail. Then I put a typo, and it failed. So I assume that default_entity_manager is defined automatically (I am not sure where).


Otherwise:

In case it doesn't work, the other fix would be to verify why the entityManager is not automatically wired. Check your config for autowiring the src/ folders.

App\:
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Tests, ....}'

and ensure that Blocks is not listed among the exclude folders.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
  • [This is another answer](https://stackoverflow.com/a/52254378/3088045) where the needed definition of `arguments:` for 3rd party services is mentioned. – Kamafeather Nov 13 '19 at 01:17