I am using Symfony 4 try to inject a parameter into an Doctrine Entity Listener
Konfiguration:
App\EventListener\MyListener:
arguments:
- "%kernel.cache_dir%"
tags:
- { name: doctrine.orm.entity_listener }
Annotation of Entity Class:
@ORM\EntityListeners({"App\EventListener\MyListener"})
Listener:
namespace App\Eventlistener;
use Doctrine\ORM\Event\LifecycleEventArgs;
class MyListener {
private $cacheDirectory;
public function __construct($cacheDirectory)
{
$this->cacheDirectory = $cacheDirectory;
}
public function postUpdate($entity, LifecycleEventArgs $args)
{
...
}
}
When updating the Entity i get the Exception: Too few arguments to function __construnt(). 0 passed in ...\vendor\doctrine\doctrine-bundle\Mapping\ContainerAwareEntityListenerResolver.php on line 76 and exactly 1 expected
I also tried setter injection, but the setter method seems never to be called.
(This is a simplified Demo - I actually will need to inject a service and use it in postUpdate)
Documentation: https://symfony.com/doc/master/bundles/DoctrineBundle/entity-listeners.html (but without DI)
UPDATE: I found this Answer but this is not working with symfony 4.