Skip to content

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

Merged
merged 1 commit into from
May 10, 2017

Conversation

bocharsky-bw
Copy link
Contributor

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:

$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

}
}

public function removeComment(Comment $comment)
{
if ($this->comments->removeElement($comment)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@bocharsky-bw bocharsky-bw May 10, 2017

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

@javiereguiluz
Copy link
Member

Thanks @bocharsky-bw.

@javiereguiluz javiereguiluz merged commit b5342c1 into symfony:master May 10, 2017
javiereguiluz added a commit that referenced this pull request May 10, 2017
…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
@stof
Copy link
Member

stof commented May 10, 2017

My suggestion would be to stop using bi-directional relations instead, whenever we can (i.e. following the official Doctrine recommendation)

@javiereguiluz
Copy link
Member

Did this recommendation changed recently? Do you have some links to docs or GitHub issues/PRs? Thanks!

@voronkovich
Copy link
Contributor

Doctrine documentation says:

If you want to make sure that your collections are perfectly encapsulated you should not return them from a getCollectionName() method directly, but call $collection->toArray(). This way a client programmer for the entity cannot circumvent the logic you implement on your entity for association management.

See http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#association-management-methods

I would suggest to use toArray method.

@voronkovich
Copy link
Contributor

@bocharsky-bw bocharsky-bw deleted the inverse-side branch May 10, 2017 16:31
@bocharsky-bw
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants