cakePHP HABTM,我理解错了吗?
我知道每一个新行都会导致之前存在的行被删除?
其背后的想法是什么?我不相信这是..
那么,我错了什么?
编辑 A
我有一个表单,将商店添加到 Stores
表中。该商店有一个名为 owner_id
的列,该列通过belongsTo 关系与Users
表关联。
还有一个名为 stores_users
的表,该表应该使用 HABTM 关系来存储每个商店的经理。
对于此表,有一个带有电子邮件字段的表单,通过将记录直接保存到 stores_users
表中,将用户连接到商店。
所以,如果我正确理解这个术语的话,任何地方都没有完整的 HABTM 保存。
所以,我的问题是:
- 我应该以这种方式使用它吗?
- 如果我的方法不正确,您能给我建议吗?
- 如何使用存储的数据,使用
$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:
- Should I expect problems using it this way?
- Can you advice me about how to it, if my method is not the proper way?
- How can I use the stored data, using
$this->User->find(...)
to get all the stores that the user can manage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这就是 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.