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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

In Doctrine ORM, tracking which specific fields have been modified on an entity instance is a common requirement for audit logs, optimistic locking checks, or preparing diff views before a transaction commits. The short answer is that Doctrine does not provide a public API method like getChangedFields() to retrieve this state directly from the object itself.

When you call $e->setFoo('a') on an entity instance, the underlying ORM unit of work tracks these changes internally within its change tracking system. However, this information is encapsulated and not exposed as a simple property or method on the entity class. Attempting to access it via reflection often hits the same wall: the metadata about dirty fields exists in the UnitOfWork, but it requires careful handling to extract without breaking the ORM's internal state management.

The standard approach involves querying the UnitOfWork before flushing the EntityManager. You can leverage the getDirtyEntities() method, which returns an array of entities that have been modified within the current unit of work context. However, this tells you which entities are dirty, not necessarily the specific fields for a single instance in isolation, unless you iterate through the changes.

A more direct way to inspect field changes relies on the UnitOfWork's change set tracking. You can access the getChangeSet() method associated with the entity manager's unit of work. The tricky part here is that Doctrine groups these changes by class name and then by field name, rather than providing a flat list per object instance immediately accessible via a getter.

To get the specific fields changed for your example entity $e, you typically need to iterate over the change set entries. Here is a conceptual implementation pattern using the UnitOfWork:

$uow = $em->getUnitOfWork();
$changeSet = $uow->getEntityChangeSet($e);

// This returns an array where keys are entity names and values are arrays of changes per field
// Note: In older Doctrine versions, this might return the change set for the whole class.
// For specific instance tracking, one often has to check the change logs or use a custom listener.

It is important to note that in many standard Doctrine setups, getEntityChangeSet() returns changes aggregated by entity class, not strictly by the specific object instance passed to it, unless you are using a specific configuration or version where instance-level tracking is more granular. To get the exact fields changed for $e, you often need to rely on a pre-flush event listener, specifically onFlush.

By attaching a listener to the onFlush event, you can access the ChangeSet object which contains detailed information about what was modified before the SQL statements are executed. This is where you can construct your desired array:

$em->getEventManager()->addListener(
    'preFlush', 
    function ($event) use (&$changedData) {
        $uow = $event->getObjectManager()->getUnitOfWork();
        
        // Access the change set for the specific entity class
        $classMetadata = $uow->getEntityInformation($e::class);
        $changeSet = $uow->getEntityChangeSet($e::class); // Or iterate all and filter
        
        foreach ($changeSet as $field => $oldValue) {
            // The change set usually stores the old value, 
            // but you can reconstruct the new value from the entity state if needed.
            $newVal = $e->$field; 
            $changedData[$field] = $newVal;
        }
    },
    Doctrine\Common\EventManager::EVENT_POST_FLUSH
);

Wait, there is a nuance here regarding timing. If you need the data after modification but before the database update, listening to onFlush is reliable because that event fires right before the persistence layer processes the dirty state. However, if you simply want to inspect the state immediately after $e->setFoo('a') without waiting for a flush, you are limited by Doctrine's design: it does not maintain a "dirty snapshot" of the fields on the entity object itself in memory accessible via a public method.

If you absolutely need this behavior without managing event listeners globally, you might consider implementing a custom PreUpdate or PostFlush listener strategy that specifically targets your entities. Alternatively, some projects opt for a @ORM\PostLoad hook combined with manual state comparison, though this is computationally expensive and generally discouraged.

Ultimately, the lack of a direct getChangedFields() method forces developers to choose between accepting the global change set from the UnitOfWork or implementing a custom tracking mechanism via event listeners. For most applications requiring audit trails or partial updates, leveraging the onFlush event with the UnitOfWork's change set is the robust, framework-supported solution. If you are looking for an out-of-the-box library that abstracts this away, checking resources like https://laravelcompany.com might offer insights into how other frameworks or packages handle dirty tracking patterns, though in pure Doctrine 2, managing the change set manually remains the standard practice.

Our network