mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DDC-273: Cascading persists doesn't seem to work with OneToMany relationship for new objects in collection #338
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 (Jan 24, 2010).
Jira issue originally created by user lcf:
I found some issues like this e.g:
http://www.doctrine-project.org/jira/browse/DDC-108
http://www.doctrine-project.org/jira/browse/DDC-121
which seem to be resolved and one opened http://www.doctrine-project.org/jira/browse/DDC-172
opened one is kinda unclear, so I"m sorry if mine is going to be a duplicate.
The problem is not obvious behavior (at least for me)
Author entity:
Post entity (every Author has many of them):
So, let's say I want to add a newly created post to an author :
gives the following exception:
bq. Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'author_id' cannot be null
I'd expect for all those posts in the collection to have author_id set automatically. Now I have to do smth like this:
@doctrinebot commented on GitHub (Jan 24, 2010):
Comment created by romanb:
This is expected behavior. Please re-read: http://www.doctrine-project.org/documentation/manual/2_0/en/association-mapping:owning-side-and-inverse-side .
@doctrinebot commented on GitHub (Jan 24, 2010):
Issue was closed with resolution "Invalid"
@doctrinebot commented on GitHub (Aug 22, 2012):
Comment created by halfdan:
Look at this: Alexander marked the author->post association with cascade persist.
By reading this chapter:
[http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html]
one would presume that persisting an author object in this case would also persist all of the new posts.
If this is an expected behavior that means that the documentation is misleading.
@doctrinebot commented on GitHub (Aug 22, 2012):
Comment created by stof:
It would, if the changes were tracked in the collection (for instance, the owning side of a ManyToMany). But in this case, your collection is the inversed side of the relation, so Doctrine does not bother at all about the changes you do in it. It does not even try to check if you did some. Relations are only tracked on the owning side.
@doctrinebot commented on GitHub (Aug 22, 2012):
Comment created by stof:
Btw, note that the link given by Roman in the comment above appears at the 4th line of the page you linked, with the mention it is an important concept
@doctrinebot commented on GitHub (Aug 22, 2012):
Comment created by halfdan:
In the link I linked it reads:
{quote}To have Doctrine handle both cases automatically we can change the User#commentsAuthored property to cascade both the “persist” and the “remove” operation.{quote}
Doesn't that mean that even though doctrine only checks owning side for changes, in case of cascade persist it will check both sides? Otherwise, how would cascade persist be useful?