kskaradzinski
kskaradzinski

Reputation: 5084

Symfony2: Doctrine subscriber throws ContextErrorException - must be an instance of Doctrine\Common\Persistence\Event\PreUpdateEventArgs

I have such listener

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

and method

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

and Doctrine throw Exception

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Certificate\MachineBundle\Event\MachineSubscriber::preUpdate() must be an instance of Doctrine\Common\Persistence\Event\PreUpdateEventArgs, instance of Doctrine\ORM\Event\PreUpdateEventArgs given,

Its strange becouse I use proper class.

Upvotes: 4

Views: 2681

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52513

You're using the wrong namespace/class to typehint the preUpdate() function argument. The correct hierarchy is:

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

Typehint with ...

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)
{
    // ...

... but NOT with:

use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;

Upvotes: 5

Related Questions