phpsymfonydoctrine-ormdoctrine

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.


Solution

  • 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();
    }