cakePHP HABTM,我理解错了吗?

发布于 2024-12-14 12:27:52 字数 562 浏览 0 评论 0原文

我知道每一个新行都会导致之前存在的行被删除?

其背后的想法是什么?我不相信这是..
那么,我错了什么?


编辑 A

我有一个表单,将商店添加到 Stores 表中。该商店有一个名为 owner_id 的列,该列通过belongsTo 关系与Users 表关联。

还有一个名为 stores_users 的表,该表应该使用 HABTM 关系来存储每个商店的经理。

对于此表,有一个带有电子邮件字段的表单,通过将记录直接保存到 stores_users 表中,将用户连接到商店。
所以,如果我正确理解这个术语的话,任何地方都没有完整的 HABTM 保存。

所以,我的问题是:

  1. 我应该以这种方式使用它吗?
  2. 如果我的方法不正确,您能给我建议吗?
  3. 如何使用存储的数据,使用 $this->User->find(...) 获取用户可以管理的所有商店?

I understood that every new row, causes the deletion of the rows that were there before?

What is the idea behind it? I don't believe that it is ..
So, what am i getting wrong?


Edit A

I have a form that adds a store to the Stores table. the store have a column named owner_id which is associated to the Users table through a belongsTo relationship.

There is also a table named stores_users that supposed to store the mangers for each store, using the HABTM relationship.

For this table there is a form with an email field, that connects the user to the store by saving the record directly to the stores_users table.
So, there is no full HABTM save anywhere, if I understand the term correctly.

So, my questions are:

  1. Should I expect problems using it this way?
  2. Can you advice me about how to it, if my method is not the proper way?
  3. How can I use the stored data, using $this->User->find(...) to get all the stores that the user can manage?

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

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

发布评论

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

评论(1

染年凉城似染瑾 2024-12-21 12:27:52

是的,这就是 cakephp 中 HABTM 的默认行为
虽然这不是“每一行”,而是“每个 HABTM 保存”。

如果您始终提供所有 HABTM 值,则此方法有效。
并根据此类 HABTM 的规范提供烘焙视图,这一切都是开箱即用的。

如果您更改默认行为(旧行为不会被删除),您将需要确保没有重复项。我认为,有一些行为试图实现这一目标。
但我建议您以 cake 的默认行为可以完成其工作的方式构建表单。

示例:

IS:1,3,6(在数据库中此键)

NEW:2,3,6(来自表单)

(蛋糕删除 1,3,6 并添加 2,3,6)

=>总体结果(忘记不同的主键):删除“1”,添加“2”,

因此它可能不是最节省资源的方法,但它肯定是最简单和最快的。

yes, thats the default behavior of HABTM in cakephp
although this is not on "every row", but "every HABTM save".

this is working IF you always provide all HABTM values.
and with baked views according to the specifications for such HABTM this is all working out of the box.

if you change the default behavior (old ones get not deleted) you will need to make sure that there are no duplicates. there are behaviors out there, I think, which try to accomplish that.
but I would recommend for you to build your forms the way that the default behavior of cake can do its job.

Example:

IS: 1,3,6 (in DB for this key)

NEW: 2,3,6 (coming from form)

(cake deletes 1,3,6 and adds 2,3,6)

=> overall result (forgetting about different primary keys): "1" deleted, "2" added

so it might not be the most resource sparing way but its sure the easiest and fastest.

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