删除HABTM关联记录 [英] Deleting HABTM Association Record

查看:75
本文介绍了删除HABTM关联记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,帖子和标签。帖子HasAndBelongsToMany标签。

I have two models, Posts and Tags. Posts HasAndBelongsToMany Tags.

我们假设帖子1有标签world,news和paper。

Let's say a Post 1 has the tags world, news, and paper.

在加入表中,我想删除标签纸和帖子1之间的关联,但是标签纸不应该从标签表中删除。

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.

如何在CakePHP中执行此操作?

How do I do this in CakePHP?

推荐答案

当您执行任何HABTM操作而不是添加一个新的时,Cake将删除并重新创建相关的连接表行。所有你需要做的是获取记录,删除标记并再次保存记录。

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.

请点击此处: http://mrphp.com.au/code/working- habtm-form-data-cakephp

这篇关于删除HABTM关联记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆