mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Refetching entity with eager relation overwrites nulled relation #6500
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @SiebelsTim on GitHub (Jul 8, 2020).
Bug Report
Summary
When setting a ManyToOne-Relation to null, and refetching the same entity, the original value of that relation is restored.
Refetching can happen, e.g. using findOneBy or even when lazily loading a collection where that entity is contained.
This problem only occurs when the fetch mode is eager.
Current behavior
Assume two entites: Bug and User. Every Bug has one optional Reporter which is a User.
Bug::reporterhasfetch="EAGER".Now when I want to clear the
reporterfield of a Bug using$bug->setReporter(null)and fetching that entity again before flushing, the bug's original reporter is restored in that bug. An eventual flush will not set the bug's reporter to null as doctrine sees an unchanged entity.How to reproduce
I prepared a reproduction case based on the doctrine tutorial here: https://github.com/SiebelsTim/doctrine-bug-repro
It is the "current behaviour"-section put into code. The README contains step-by-step instructions on how to reproduce this. The latest commit shows the changes I made for this.
This only happens when
fetch=EAGERand when setting the reporter tonull. Setting a different non-null value will not be overwritten.As a result, I think seemingly valid code is prone to this problem:
Expected behavior
Fetch mode and lazy-loading does not reset changes made to entities.
@flaushi commented on GitHub (Sep 17, 2020):
I rather think this is no bug but a feature. If you want to set reporter to null, do it and flush it. After flushing, a refetch should give correct results.
@SiebelsTim commented on GitHub (Sep 23, 2020):
But why would the fetch mode in the entity change the behaviour?