Symfony2:Doctrine订阅器抛出ContextErrorException - 必须是Doctrine\Common\Persistence\Event\PreUpdateEventArgs的实例

4
我有一个这样的监听器。
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Events;

class MachineSubscriber implements EventSubscriber

和方法

/**
     * @param PreUpdateEventArgs $args
     */
    public function preUpdate(PreUpdateEventArgs $args)

并且Doctrine抛出异常

ContextErrorException: 可捕获的致命错误:传递给 Certificate\MachineBundle\Event\MachineSubscriber::preUpdate() 的第一个参数必须是 Doctrine\Common\Persistence\Event\PreUpdateEventArgs 的实例,但给定了 Doctrine\ORM\Event\PreUpdateEventArgs 的实例。

很奇怪,因为我使用了正确的类。

1个回答

5

您在 preUpdate() 函数参数的类型提示中使用了错误的命名空间/类。正确的层次结构如下:

Doctrine\Common\EventArgs
|_ Doctrine\ORM\Event\LifecycleEventArgs
  |_ Doctrine\ORM\Event\PreUpdateEventArgs

使用Typehint进行...

use Doctrine\Common\EventArgs;

public function preUpdate(EventArgs $args)
{
    // ...

... or ...

use Doctrine\ORM\Event\LifecycleEventArgs;

public function preUpdate(LifecycleEventArgs $args)
{
    // ...

... or ...

use Doctrine\ORM\Event\PreUpdateEventArgs;

public function preUpdate(PreUpdateEventArgs $args)
{
    // ...

...但不包括:

use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接