删除HABTM关联记录

发布于 2024-09-29 06:34:56 字数 166 浏览 0 评论 0原文

我有两个模型,帖子和标签。帖子有并属于许多标签。

假设帖子 1 具有世界、新闻和报纸标签。

现在在连接表中,我想删除标签“paper”和Post 1 之间的关联,但标签“paper”不应该从标签表中删除。仅应删除连接表中的关联。

我如何在 CakePHP 中执行此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2024-10-06 06:34:56

当您执行除添加新操作之外的任何 HABTM 操作时,Cake 会删除并重新创建关联的连接表行。您所要做的就是获取记录、删除标签并再次保存记录。

在视图中,我将标签写入一个表单输入字段。保存记录时,我处理该字段的值以提取标签(按逗号或空格或其他方式分解),然后保存标签。

这里有一个很好的指南: http://mrphp.com.au/代码/working-habtm-form-data-cakephp

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文