Initializing a collection overwrites nulled field #7408

Open
opened 2026-01-22 15:51:20 +01:00 by admin · 0 comments
Owner

Originally created by @insel-maz on GitHub (Aug 13, 2024).

Bug Report

Q A
BC Break no?
Version 3.2.1 down to 2.x

Summary

When Doctrine fetches the entities for a PersistentCollection it overwrites the programmatically set null values in association fields for already managed (in the identity map) entities.

This happens by the if statement in 205b2f5f20/src/Internal/Hydration/ObjectHydrator.php (L434).

Current behavior

The association field, which was just set to null by code, is overwritten with the persisted peer entity.

How to reproduce

In the following example there is a one-to-one association. One side gets set to null, but when a collection containing the entity is loaded, the field is restored with the peer entity.

https://github.com/insel-maz/doctrine-bug-collection-initialization-overwrites-field

Expected behavior

If an entity is already managed (in the identity map) Doctrine should not change the fields when fetching the database records for a PersistentCollection.

Originally created by @insel-maz on GitHub (Aug 13, 2024). ### Bug Report | Q | A |------------ | ------ | BC Break | no? | Version | 3.2.1 down to 2.x #### Summary When Doctrine fetches the entities for a PersistentCollection it overwrites the programmatically set null values in association fields for already managed (in the identity map) entities. This happens by the if statement in https://github.com/doctrine/orm/blob/205b2f5f200c63f8cb22a26bfc0174071ed97d2c/src/Internal/Hydration/ObjectHydrator.php#L434. #### Current behavior The association field, which was just set to null by code, is overwritten with the persisted peer entity. #### How to reproduce In the following example there is a one-to-one association. One side gets set to null, but when a collection containing the entity is loaded, the field is restored with the peer entity. https://github.com/insel-maz/doctrine-bug-collection-initialization-overwrites-field #### Expected behavior If an entity is already managed (in the identity map) Doctrine should not change the fields when fetching the database records for a PersistentCollection.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7408