0

I have two tables with a ManyToMany relationship. I needed to add some extra fields to this relationship, so I created a new entity that has a ManyToOne relationship with each of the two tables. The issue I'm having is with removing the relationship entity. I don't want to remove entries in either of the two tables, I just want to remove the relationship. What I've done to set up the relating entity is pretty much described here:

Doctrine2: Best way to handle many-to-many with extra columns in reference table

To remove the relationship, I've tried removing the relating element from each of the two other table entries, and then setting the reference to those tables to null in the relating entity. That doesn't seem to work; Doctrine tries to execute a query to update the entry in the relating table and set both the foreign keys to null. I would expect it to remove the entry in the relating table, if all references to it have been removed.

Let me know if you'd like to see my entities, or if this makes enough sense as it stands.

Community
  • 1
  • 1
Cully
  • 6,427
  • 4
  • 36
  • 58

1 Answers1

0

Actually, deleting the entity that acts as "join-table" does the trick. Nothing should be cascaded.

So assuming you have a relation like

User <- UserGroup -> Group

You just need to remove the UserGroup entity.

If you remove User or Group, and you have set cascade persist operations correctly, also UserGroup will go. You can also use orphanRemoval to avoid having UserGroup assigned to different User or Group elements.

Ocramius
  • 25,171
  • 7
  • 103
  • 107
  • Yup, I just had to call em->remove() on the join-table. Worked great. Thanks for the answer, and for understanding my question :) – Cully Jul 23 '12 at 20:15