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
-
I have 2 entities in a relationship like this:
and
My question is, should I update the
setDuel
function to this:The reason is that in my code, I was creating new
Game
object 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
Games
Collection
was empty.I had to change
$game->setDuel($duel);
to$duel->addGame($game);
, which correctly sets both sides of the relationship on both theGame
and theDuel
.So by the same logic, should my setter on
Game
also update the relationship onDuel
instance?The symfony maker bundle does generate correctly updating collections on the
OneToMany
side, but not on theManyToOne
, is there a reason for this?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions