Symfony 4 Doctrine LifecycleEventArgs getEntity() vs getObject()

Viewed 904

What is the difference between LifecycleEventArgs::getObject() and LifecycleEventArgs::getEntity()?

namespace App\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;

/**
 * Class MyListener
 *
 * @package App\EventListener
 */
class MyListener implements EventSubscriber
{
    /**
     * @return array|string[]
     */
    public function getSubscribedEvents()
    {
        return [
            Events::postUpdate,
        ];
    }

    /**
     * @param LifecycleEventArgs $event
     */
    public function postUpdate(LifecycleEventArgs $event)
    {
        $entity = $event->getEntity();
        $object = $event->getObject();

        $entity === $object; //true...
    }
}

So far as I can tell these two methods return the exact same object, ie they point to the same instance of a given Entity.

Is that always the case?

Should one be used over the other or does it not matter?

1 Answers

There is no difference. The getObject() method comes from the parent class of the LifecycleEventArgs class which is provided by the doctrine/persistence package.

The base event class is mainly helpful when you want to build an integration layer for several Doctrine implementations (e.g. ORM and ODM) and in which case you would use getObject().

Related