【问题标题】:Symfony - Dependencies not injecting in my entity listener classSymfony - 依赖项未注入我的实体侦听器类
【发布时间】:2018-04-15 05:13:54
【问题描述】:

我的自动装配不适用于基本文件实体侦听器。

我有一个 File 实体类,它使用注释来指定侦听器,例如:

/**

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

FileEntityListener 类开始如下:

class FileEntityListener
{
private $encoderFactory;
private $logger;

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

当监听器启动时,依赖项没有注入到监听器的构造函数中,构造函数出现错误,说:

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

包是自动装配的,但依赖项没有被注入。

有人知道为什么这不起作用吗?也许我缺少一个轻微的配置步骤?

【问题讨论】:

标签: php symfony dependencies code-injection autowired


【解决方案1】:

您需要在 services.yaml 中像这样标记您的实体侦听器:

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

【讨论】:

  • 附言。如果您要使用此解决方案,请不要使用 @EntityListeners 注释实体。
  • 当我们在 doctrine.yaml 中设置了 Doctrine 缓存时,这对我们不起作用。
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 2018-12-07
  • 2014-08-23
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
相关资源
最近更新 更多