Is there a built-in way to get all of the changed/updated fields in a Doctrine 2 entity?

Yes, Doctrine 2 provides a built-in way to get the changed fields in an entity. The UnitOfWork class, which is responsible for tracking changes to entities, has a method called getEntityChangeSet() which returns an array of changes made to a given entity. The array keys are the field names and the values are an array with the old and new values of the field.

Watch a course Learn object oriented PHP

For example, you can use this method like this:


$entity = // ...
$uow = $entityManager->getUnitOfWork();
$changeSet = $uow->getEntityChangeSet($entity);

You can also use the method getScheduledEntityUpdates() to get all entities that have been scheduled for update during a flush operation.