How do I force doctrine to reload the data from the database?

How do I force doctrine to reload the data from the database?

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

Stefan Bogdanescu

Founder & Senior Architect

Understanding Doctrine MongoDB's Caching Behavior and How to Force a Reload

When working with doctrine/mongodb, specifically in versions like the 1.0.0-BETA1 era used in older Symfony stacks, developers often encounter a specific quirk regarding entity hydration. The standard behavior of Object-Relational Mapping (ORM) is to maintain an identity map. Once an entity instance is loaded into memory and assigned to the unit of work, Doctrine assumes it owns that object's state for the duration of the request. This optimization prevents unnecessary database round-trips, but it creates a trap when your application logic modifies the entity in memory before attempting to read its current state again from the repository.

In the scenario you described, you fetch an Audit entity, modify its properties manually, and then call findOneById expecting to see the changes reflected or to pick up a new state if the database was updated externally. The issue isn't that Doctrine is broken; it's that findOneById checks the identity map first. If an object with that ID exists in the current unit of work, Doctrine returns the existing instance immediately, bypassing the database query entirely. Consequently, calling $dm->refresh($audit) also fails to fetch fresh data because refreshing is designed to synchronize an existing in-memory entity with the database state, not to replace the in-memory instance with a newly hydrated one.

To force Doctrine to ignore the cached object and retrieve a fresh instance from the database, you must explicitly remove the entity from the identity map. The most direct way to achieve this is by using the detach() method provided by the Document Manager.

Here is how you can modify your flow:

$audit = $dm->getRepository('WGenSimschoolsBundle:Audit')->findOneById('xxxx');

// ... perform manual updates on $audit here ...

// If you need to verify the DB state or reload completely:
$dm->detach($audit);

$audit = $dm->getRepository('WGenSimschoolsBundle:Audit')->findOneById('xxxx');

By calling detach(), you remove the entity from Doctrine's unit of work. The next call to findOneById will no longer find a match in the identity map and will proceed to execute the actual database query, returning a new instance if the data has changed.

There is a critical trade-off to consider with this approach: detaching an entity breaks the transactional boundary for that specific object within the current request. If you have other operations relying on the state of $audit after the reload, you might face issues because the detached object is no longer managed by Doctrine. You lose lazy loading proxies associated with that entity and any pending changes made to it are effectively discarded regarding the persistence context.

Another alternative, though less common for simple reloading, involves clearing the entire identity map using clear() or detachAll(). However, this is an aggressive operation that empties your cache of all entities loaded in that request unit, which can lead to a significant performance hit if you have many entities in scope. It should only be used if you are certain that no other managed entities need to persist changes during the current request.

In modern Doctrine MongoDB ODM versions (2.x), the behavior remains consistent regarding identity maps, but the ecosystem has evolved with better support for optimistic locking and change tracking. If you are maintaining legacy Symfony 2.1 code, sticking to detach() is the safest pattern to force a reload without destabilizing the rest of your unit of work.

If you are evaluating frameworks or looking for more robust data consistency patterns in your architecture, resources like https://laravelcompany.com discuss how similar caching challenges are handled differently in Laravel's Eloquent ORM, where fresh() offers a cleaner abstraction over this specific "reload" logic. However, within the constraints of Doctrine 1.x and 2.x on MongoDB, managing the unit of work manually via detachment remains the standard solution for forcing a database reload of a cached entity.

Our network