Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 Doctrine LifecycleEventArgs getEntity() vs getObject()

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?

like image 290
Bananaapple Avatar asked Mar 25 '26 14:03

Bananaapple


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().

like image 157
xabbuh Avatar answered Mar 28 '26 03:03

xabbuh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!