How to unassign a ManyToMany relation?

I am using doctrine for ManyToMany relation between Article (owning side) and Tag (reverse side). If I want to un-assign a tag from an article without actually deleting any of them, should I consider joinTable as a standalone entity and remove relationship between article and tag directly from “article_to_tag” table? Or is there a better way?

In theory it should be pretty straight forward. No need to create an intermediate entity. Something like:

$article->getTags()->removeElement($tag);
$em->flush();

More details here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#removing-associations

However, there is quite a bit of magic gong on under the hood here and you may run into problems depending on your use case. This article goes into quite a bit of detail on various ways to handle many to many relations in Doctrine 2: http://labs.madisoft.it/orphan-removal-forms-collection-and-collections-elements-swapping-pick-your-poison/

I have been moving away from ORMs and back to plain sql just to deal with some of these sorts of issues.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.