Fabio B. Silva
added a comment - 28/Apr/13 8:57 PM Benjamin Eberlei , 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 ?

Benjamin Eberlei
added a comment - 01/May/13 8:48 AM Fabio B. 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.

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?

Oleg Namaka
added a comment - 01/May/13 12:22 PM - edited 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?

Cory Close
added a comment - 28/Nov/13 1:48 AM 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.

Romaric Drigon
added a comment - 30/May/14 9:19 AM I can confirm this issue.
My use case (which I guess is pretty standard):
I deserialize a new entity (with relationships to already existing ones)
I merge it to attach existing entities
I persist it
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.

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.

Cory Close
added a comment - 30/May/14 2:47 PM 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.

+ on this one. I also have a similar situation where I get a detached entity from an API and use merge to update any changed elements. Obviously there is a workaround (trigger the lifecycle's function manually), but would be great not to have to do this.

Chris Johnson
added a comment - 14/Sep/14 11:45 PM + on this one. I also have a similar situation where I get a detached entity from an API and use merge to update any changed elements. Obviously there is a workaround (trigger the lifecycle's function manually), but would be great not to have to do this.