Deprecate EntityManager::clear with argument of one entity class and EntityRepository::clear #6623

Closed
opened 2026-01-22 15:35:51 +01:00 by admin · 2 comments
Owner

Originally created by @beberlei on GitHub (Feb 6, 2021).

It is possible to call EntityManager::clear($entity) or EntityRepository::clear() to only clear one kind of entity from the identity map. this is done by calling detach on the entity instead of actually clearing the identity map and other related UnitOfWork data.

Because clearing is not semantically the same as detaching an entity, it is not possible to do this anymore.

Alternative: If you need to detach many entities, you can perform this operation directly using the UnitOfWork. Potentially hide this away in a service or repository:

$unitOfWork = $entityManager->getUnitOfWork();
$entities = $unitOfWork->getIdentityMap()[Entity::class] ?? [];

foreach ($entities as $entity) {
    $entityManager->detach($entity);
}
Originally created by @beberlei on GitHub (Feb 6, 2021). It is possible to call `EntityManager::clear($entity)` or `EntityRepository::clear()` to only clear one kind of entity from the identity map. this is done by calling detach on the entity instead of actually clearing the identity map and other related `UnitOfWork` data. Because clearing is not semantically the same as detaching an entity, it is not possible to do this anymore. Alternative: If you need to detach many entities, you can perform this operation directly using the UnitOfWork. Potentially hide this away in a service or repository: ```php $unitOfWork = $entityManager->getUnitOfWork(); $entities = $unitOfWork->getIdentityMap()[Entity::class] ?? []; foreach ($entities as $entity) { $entityManager->detach($entity); } ```
admin added the Deprecation label 2026-01-22 15:35:51 +01:00
admin closed this issue 2026-01-22 15:35:51 +01:00
Author
Owner

@gempir commented on GitHub (Jun 21, 2021):

The documentation for batch processing will need an update then

https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html#iterating-results

@gempir commented on GitHub (Jun 21, 2021): The documentation for batch processing will need an update then https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html#iterating-results
Author
Owner

@derrabus commented on GitHub (May 11, 2022):

Fixed via #9471.

@derrabus commented on GitHub (May 11, 2022): Fixed via #9471.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6623