首页 文章

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

提问于
浏览
2

我有这样的倾听者

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()的参数1必须是Doctrine \ Common \ Persistence \ Event \ PreUpdateEventArgs的实例,Doctrine \ ORM \ Event \ PreUpdateEventArgs的实例,

奇怪的是因为我使用了正确的课程 .

1 回答

  • 3

    您正在使用错误的命名空间/类来键入 preUpdate() 函数参数 . 正确 hierarchy 是:

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

    Typehint与......

    use Doctrine\Common\EventArgs;
    
    public function preUpdate(EventArgs $args)
    {
        // ...
    

    ... 要么 ...

    use Doctrine\ORM\Event\LifecycleEventArgs;
    
    public function preUpdate(LifecycleEventArgs $args)
    {
        // ...
    

    ... 要么 ...

    use Doctrine\ORM\Event\PreUpdateEventArgs;
    
    public function preUpdate(PreUpdateEventArgs $args)
    {
        // ...
    

    ...但是 NOT

    use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
    

相关问题