Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the object id of an entity during the postRemove event?

I have something like this:

public function postRemove(LifecycleEventArgs $args)
{
    $entity = $args->getObject();
    $entity->getId();
}

$entity has every property from the selected table except id, which is always null

I expect it to have the id of the item that I am removing.

like image 770
prgrm Avatar asked Oct 30 '25 09:10

prgrm


1 Answers

If you already removed the object, it no longer has an id.

You may want to use preRemove where the id is still available.

You could even use preRemove to store the id in a non-mapped property of the object, and then get that id from the object on postRemove.

function preRemove(LifecycleEventArgs $args) {
   $object = $args->getObject();
   $object->storeId($object->getId());
}
function postRemove(LifecycleEventArgs $args) {
   $object = $args->getObject();
   $id     = $object->getStoredId();
}
like image 181
yivi Avatar answered Nov 02 '25 00:11

yivi



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!