Should I update the other side's collection in a OneToMany relationship setter? #10419
Unanswered
michnovka
asked this question in
Support Questions
Replies: 1 comment
-
|
I don’t know anything about the Maker bundle. But, if you want both sides of the association to be in sync, you have to keep them in sync. if you can make sure that all new entities will always be added on the same side of the association only, you only need to sync from there. If you can do both, set the duel on the game and add the game to the duel, you need both. This is also the reason why you should really think about which associations and directions are necessary, so you don’t code what you don’t need. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have 2 entities in a relationship like this:
and
My question is, should I update the
setDuelfunction to this:The reason is that in my code, I was creating new
Gameobject and persisting it like this (all part of a transaction)and later, before the transaction was committed, I attempted to retrieve the duels
and of course, the
GamesCollectionwas empty.I had to change
$game->setDuel($duel);to$duel->addGame($game);, which correctly sets both sides of the relationship on both theGameand theDuel.So by the same logic, should my setter on
Gamealso update the relationship onDuelinstance?The symfony maker bundle does generate correctly updating collections on the
OneToManyside, but not on theManyToOne, is there a reason for this?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions