What you're trying to do here is not possible with a Lifecycle callback (i.e. @PrePersist
) if you save only the child afterwards.
Doctrine only tracks and saves changes to the owning side of a relation.
When a bidirectional assocation is updated, Doctrine only checks on one of both sides for these changes. This is called the owning side of the association.
Therefore you can't persist an update of the parent from the child by persisting the child only.
You can find more information about the concepts of the inverse and owning side in this answer and in the documentation chapter Working with Associations.
To solve this issue ... you can either persist the parent instead of the child ... or (instead of using the lifecycle callback @PrePersist
) create an event listener that will automatically persist the parent. This is a better practice in general because it keeps application logic out of your model.
The documentation chapter How to Register Event Listeners and Subscribers provides all the necessary information to get you started.