Deleting HABTM Association Record

只谈情不闲聊 提交于 2019-12-07 12:41:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!