开发者

Deleting HABTM Association Record

开发者 https://www.devze.com 2023-01-21 11:45 出处:网络
I have two models, Posts and Tags.Posts HasAndBelongsToMany Tags. Let\'s say a Post 1 has the tags world, news, and paper.

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?开发者_如何学C


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号