简体   繁体   中英

Doctrine2 : Proper way to remove many to many association

I have a many to many relationship between two entities, I am using the cli tool to generate the entities from yaml configs, the problem is that there is no method to delete the associations.

What would be the best way to delete the associations. I can add the method to the entities, is there a better way to do this?

In one application we had a class User which could be in one or more Categories which was mapped as a many-to-many relation.

I didnt find a absolute clean way to remove a User from a Category, but what I did was:

  • get all Categories from a User
  • remove the Category which was deleted by the User
  • set the remaining Categories as a NEW ArrayCollection
  • persist the User instance (might not be necessary as Doctrine might notice that the User instance was changed and is therefore "dirty")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM