【问题标题】:Symfony2: Doctrine subscriber throws ContextErrorException - must be an instance of Doctrine\Common\Persistence\Event\PreUpdateEventArgsSymfony2:Doctrine 订阅者抛出 ContextErrorException - 必须是 Doctrine\Common\Persistence\Event\PreUpdateEventArgs 的一个实例
【发布时间】:2014-09-11 10:53:39
【问题描述】:
我有这样的听众
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:可捕获的致命错误:参数 1 传递给
Certificate\MachineBundle\Event\MachineSubscriber::preUpdate() 必须是
Doctrine\Common\Persistence\Event\PreUpdateEventArgs 的一个实例,
给出 Doctrine\ORM\Event\PreUpdateEventArgs 的实例,
这很奇怪,因为我使用了正确的类。
【问题讨论】:
标签:
symfony
doctrine-orm
doctrine
【解决方案1】:
您使用了错误的命名空间/类来输入preUpdate() 函数参数。正确的 hierarchy 是:
Doctrine\Common\EventArgs
|_ Doctrine\ORM\Event\LifecycleEventArgs
|_ Doctrine\ORM\Event\PreUpdateEventArgs
使用 ... 输入提示
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)
{
// ...
...但不有:
use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;