Overwriting entities relation field when hydrating a collection #6913

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

Originally created by @mlebkowski on GitHub (Jan 25, 2022).

Bug Report

Doctrine needlessly overwrites entities relation field in certain circumstances.

The scenario is described in details here: https://github.com/mlebkowski/doctrine-bug

I have no idea what’s happening, but I was able to reproduce the bug in as narrow of a scenario as I could.

Q A
BC Break no
Version 2.11.0

Summary

See the related code in: https://github.com/mlebkowski/doctrine-bug

Prerequisites

  • Two entities in one-to-many relation (Author and Book in this example)
  • This is where the bug manifests itself: The „many” side of the relation has a many-to-one relation to a yet another entity (Book::$owner to User)
  • The aforementioned relation is eagerly loaded (src/User.php:21)
  • Another, unrelated entity has a same many-to-one relation (Account::$user to User). As far as I understand the bug, this is only required, so we get a proxy of the entity instead of the entity itself.

Test scenario

  • There is one of each entity in the database. The bug manifests itself during hydration
  • We fetch the Book entity using a non-pk field (Book::$isbn)
  • We fetch the Account (container) class to get the User entity as a proxy
  • We assign the user entity to a previously empty User::$owner field
  • We fetch the Author::$books collection, and hydrate it (toArray())

Current behavior

The Book::$owner field is null

How to reproduce

https://github.com/mlebkowski/doctrine-bug

Expected behavior

The Book::$owner is not null and holds the previously assigned User proxy entity

Originally created by @mlebkowski on GitHub (Jan 25, 2022). ### Bug Report Doctrine needlessly overwrites entities relation field in certain circumstances. The scenario is described in details here: https://github.com/mlebkowski/doctrine-bug I have no idea what’s happening, but I was able to reproduce the bug in as narrow of a scenario as I could. | Q | A |------------ | ------ | BC Break | no | Version | 2.11.0 #### Summary See the related code in: https://github.com/mlebkowski/doctrine-bug **Prerequisites** * Two entities in one-to-many relation (`Author` and `Book` in this example) * **This is where the bug manifests itself**: The „many” side of the relation has a many-to-one relation to a yet another entity (`Book::$owner` to `User`) * The aforementioned relation is eagerly loaded (`src/User.php:21`) * Another, unrelated entity has a same many-to-one relation (`Account::$user` to `User`). As far as I understand the bug, this is only required, so we get a proxy of the entity instead of the entity itself. **Test scenario** * There is one of each entity in the database. The bug manifests itself during hydration * We fetch the `Book` entity using a non-pk field (`Book::$isbn`) * We fetch the `Account` (container) class to get the `User` entity as a proxy * We assign the user entity to a previously empty `User::$owner` field * We fetch the `Author::$books` collection, and hydrate it (`toArray()`) #### Current behavior The `Book::$owner` field is `null` #### How to reproduce https://github.com/mlebkowski/doctrine-bug #### Expected behavior The `Book::$owner` is not null and holds the previously assigned `User` proxy entity
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6913