-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
RFC: Always enforce updating inverse side for Comment #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
} | ||
} | ||
|
||
public function removeComment(Comment $comment) | ||
{ | ||
if ($this->comments->removeElement($comment)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bocharsky-bw, this code checks that a comment belongs to a post. See http://www.doctrine-project.org/api/common/2.3/class-Doctrine.Common.Collections.Collection.html#_removeElement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're correct. But here's another example to show that this code has a bit unexpected behavior:
$comment = new Comment();
$post->addComment($comment); // Comment::$post === $post object
$post->getComments()->removeElement($comment);
$post->removeComment($comment); // Comment::$post is still equal to $post object here due to the failed check in this method
Thanks @bocharsky-bw. |
…harsky-bw) This PR was merged into the master branch. Discussion ---------- RFC: Always enforce updating inverse side for Comment It's an edge case, but what do you think about always updating inverse side in adder/remover methods? For example, currently this code won't update inverse side properly: ```php $comment = new Comment(); $post->getComments()->add($comment); // but then if we will call adder method $post->addComment($comment); // Comment::$post is still equals null here due to the failed `!contains()` check in it ``` Commits ------- b5342c1 Always enforce updating inverse side for Comment
My suggestion would be to stop using bi-directional relations instead, whenever we can (i.e. following the official Doctrine recommendation) |
Did this recommendation changed recently? Do you have some links to docs or GitHub issues/PRs? Thanks! |
Doctrine documentation says:
I would suggest to use |
@javiereguiluz, I think @stof means doctrine best practices http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/best-practices.html#constrain-relationships-as-much-as-possible |
If we can't or don't want to get rid of bi-directional relations as @stof suggested, then @voronkovich idea makes sense I think. Looks like with bi-directional relations there's no perfect solution. |
It's an edge case, but what do you think about always updating inverse side in adder/remover methods?
For example, currently this code won't update inverse side properly: