Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony - Dependencies not injecting in my entity listener class

My autowiring is not working for a basic file entity listener.

I have a File entity class which uses annotations to specify the listener, like:

/**

@Orm\Entity(repositoryClass="root\CoreBundle\Repository\FileRepository")
@Orm\EntityListeners({"root\CoreBundle\EventListener\Entity\FileEntityListener"})
@Orm\Table(
etc...

The FileEntityListener class starts off as follows:

class FileEntityListener
{
private $encoderFactory;
private $logger;

public function __construct(FilesystemMap $filesystemMap, LoggerInterface $logger)
{
    $this->setFilesystemMap($filesystemMap);
    $this->logger = $logger;
}

When the listener kicks in, the dependencies aren't injecting into the constructor of the listener and I get an error for the constructor, saying:

Type error: Too few arguments to function Epcvip\CoreBundle\EventListener\Entity\FileEntityListener::__construct(), 0 passed in /var/www/html/accounting/vendor/doctrine/doctrine-bundle/Mapping/ContainerAwareEntityListenerResolver.php on line 83 and exactly 2 expected

The bundle is autowired and yet the dependencies are not being injected.

Would anyone know why this isn't working? Maybe a slight configuration step I am missing?

like image 677
Brent Heigold Avatar asked Nov 17 '17 01:11

Brent Heigold


1 Answers

You need to tag your entity listener like this in services.yaml:

services: root\CoreBundle\EventListener\Entity\FileEntityListener tags: - { name: doctrine.orm.entity_listener }

like image 177
Vovkin Avatar answered Sep 22 '22 12:09

Vovkin