mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-2406: Merging of new detached entities with PrePersist lifecycle callback breaks #3020
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 @doctrinebot on GitHub (Apr 19, 2013).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user deatheriam:
Merging of new detached entities with PrePersist lifecycle callback breaks:
Code snippet:
The reason it happens is that the merge operation is trying to persist a new entity created by uow::newInstance($class) without populating its properties first:
This should happen first for the $managedCopy:
@doctrinebot commented on GitHub (Apr 28, 2013):
Comment created by @FabioBatSilva:
[~beberlei], Is this an expected behavior ?
I mean.. This issue is about dispatch the event before copy the original values into the managed instance.
But overall, should *$em->detach()* trigger @PrePersist events ?
@doctrinebot commented on GitHub (May 1, 2013):
Comment created by @beberlei:
[~fabio.bat.silva] he talks about $em->merge() on a detached entity calling pre persist. This should only happen on a NEW entity, not on a DETACHED one.
@doctrinebot commented on GitHub (May 1, 2013):
Comment created by deatheriam:
I tend to disagree with the statement above about pre persist that should not happen on a detached entity being merged back in. If this event handler contains a business logic that this entity needs to be checked against and the detached entity was modified before the merge operation in a way that invalidates it in the prePersist than I will end up with the invalid entity in the identity map. If the merge operation calls persist it must run the prePersist event handler as well for consistency.
If there is a logic that prevents persisting invalid entities why should it bypassed in the merge operation?
@doctrinebot commented on GitHub (Nov 28, 2013):
Comment created by fodagus:
I can confirm that this bug has not been fixed while using doctrine 2.4
Exactly as Oleg Namaka has described, my organization is trying to use @PrePersist callbacks to enforce validation on new entities.
However, we use an extensive client side framework that sends json back to our server. Our workflow is then:
deserializeJson into detached entity
merge detached entity to get it managed (this will apply our edits to an existing entity, or create a new one if this one is new)
persist
However, some entities run into the above problem while using this workflow, so our validation is not run. I can provide more code samples if required.
@doctrinebot commented on GitHub (Apr 29, 2014):
Comment created by deatheriam:
Whose feedback is it awaiting for?
@doctrinebot commented on GitHub (May 30, 2014):
Comment created by romaricdrigon:
I can confirm this issue.
My use case (which I guess is pretty standard):
PrePersist is called on an entity with all properties set to null. Any modifications made inside this entity won't be saved to the DB.
@doctrinebot commented on GitHub (May 30, 2014):
Comment created by fodagus:
Is there any reason the proposed solution can't be implemented? I've patched a local version of Doctrine with the proposed changes and ran it through my application's tests without any issues, so I believe this would fix the problem.
@doctrinebot commented on GitHub (Sep 14, 2014):
Comment created by chrissjohnson00:
@mhallnoinc commented on GitHub (Sep 23, 2016):
did anyone figure this out?
@cclose commented on GitHub (Mar 29, 2018):
I think this might be fixed by https://github.com/doctrine/doctrine2/pull/6177