mirror of
https://github.com/doctrine/orm.git
synced 2026-04-25 07:28:04 +02:00
DDC-121: Persisting added children to parent in a one-to-many relationship fails #155
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 (Nov 6, 2009).
Jira issue originally created by user reinier.kip:
Affects Doctrine 2.0 at revision 6679 (trunk).
Persisting children that were added to a parent retrieved from the database in (at least) a one-to-many relationship fails when the children array was not initialized before adding. A simple demonstration:
Let's retrieve our existing customer and add a user to it.
At this point the user has not been persisted.
When initializing the users collection by e.g.:
... this is not the case.
@doctrinebot commented on GitHub (Nov 6, 2009):
Comment created by romanb:
Improved formatting.
@doctrinebot commented on GitHub (Nov 7, 2009):
Comment created by romanb:
I fixed your example, UnitOfWork#_doPersist was using foreach() on the PersistentCollection so that it was initialized. That should not happen.
At the same time I think there might still be the issue of new objects in collections being wiped out when the collection is initialized. So I still need to look into that.
@doctrinebot commented on GitHub (Nov 7, 2009):
Comment created by romanb:
By the way: In your example you do not even need to call $em->persist($customer); because $customer is managed and $user is a new object attached to it (in the collection). Together with cascade={"persist"} this means it will be persisted automatically. This is called "persistence by reachability".
@doctrinebot commented on GitHub (Nov 8, 2009):
Issue was closed with resolution "Fixed"
@doctrinebot commented on GitHub (Nov 9, 2009):
Comment created by reinier.kip:
Thanks a bundle!