cakePHP HABTM,我弄错了吗? [英] cakePHP HABTM, am I getting it all wrong?

查看:60
本文介绍了cakePHP HABTM,我弄错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解每个新行都会导致以前的行被删除?

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?

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

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.

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

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

此表有一个带有电子邮件字段的表单,该表单通过将记录直接保存到 stores_users 将用户连接到商店。 code>表。

因此,如果我正确理解该术语,那么任何地方都没有完整的HABTM保存。

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.

所以,我的问题是:


  1. 我应该以这种方式遇到问题吗?

  2. 您能给我一些建议吗

  3. 如何使用 $ this-> User-> find(。)使用存储的数据? ..)获取用户可以管理的所有商店?

  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?


推荐答案

是的,那是cakephp
中HABTM的默认行为,尽管它不是每行,而是每一次HABTM保存。

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

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

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.

示例:

IS:1,3,6(在DB中为此密钥)

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

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

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

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

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

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

=> 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.

这篇关于cakePHP HABTM,我弄错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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