I have created an event listener to send email if entity name has updated. My question is, I’d like know what properties have changed (e.g), $entity->getName() = “Jim” became “Jimmy” ?
/!\ I'll detect only changed properties.
public function postUpdate(Entity $entity, LifecycleEventArgs $event): void {
// “Jim”
// Now is “Jimmy”
}
Thanks
CodePudding user response:
$event->get(Entity|Object)Manager()->getUnitOfWork()->getEntityChangeSet($entity)
CodePudding user response:
I think it would better to use a preUpdateEvent in your case.
Doctrine\ORM\Event\PreUpdateEventArgs
contains a few method that would help your in your use case.
For example hasChangedField, getNewValue and getOldValue.
/**
* Checks if field has a changeset.
*
* @param string $field
*
* @return bool
*/
public function hasChangedField($field)
{
return isset($this->entityChangeSet[$field]);
}
So you could replace your postUpdate listener with a preUpdate listener:
public function preUpdate(Entity $entity, PreUpdateEventArgs $args): void {
if($args->hasChangedField('name')){
$newName = $args->getNewValue('name');
$oldName = $args->getOldValue('name');
}
}
Otherwise if you want to keep your postUpdate listener, you will need use the unit of work to find your entity changeset and see if the name field has changed.