问题
I have two models, Posts and Tags. Posts HasAndBelongsToMany Tags.
Let's say a Post 1 has the tags world, news, and paper.
Now in the joining table, I want to remove the association between the Tag "paper" and Post 1, but the Tag "paper" should not be deleted from the tags table. Only the association in the joining table should be deleted.
How do I do this in CakePHP?
回答1:
When you do any HABTM operation other than adding a new one, Cake deletes and recreates the associated join table rows. All you have to do is get the record, remove the tag and save the record again.
In the view I write the tags into one form input field. When the record is saved, I process the value of this field to extract the tags (explode by comma or space or whatever) then save the tags.
There is a good guide here: http://mrphp.com.au/code/working-habtm-form-data-cakephp
来源:https://stackoverflow.com/questions/4006052/deleting-habtm-association-record