HABTM-Relation :为所有其他记录创建关系

发布于 2024-08-07 05:54:00 字数 296 浏览 3 评论 0原文

如果我想向TABLE A添加一条记录,有没有一种有效的方法来添加 JOIN TABLE 中的记录是否适用于 TABLE B 中的许多(或全部)记录?

我尝试构建一个简单的任务管理(在 CakePHP 中)。用户添加一个任务,然后 将添加与同一组中的每个其他用户的连接 当前用户。

目前,我使用 find('list') 方法来检索 ID 并 将它们存储在变量中。但我认为如果团体不断壮大,PHP 缓存不会在单个变量中处理如此多的数据。

If i want to add a record to TABLE A, is there an efficient way to add
a record in the JOIN TABLE for many (or all) records in TABLE B?

I try to build a simple task management (in CakePHP). An user adds a task and there
will be added a connection to each other user in the same group as the
current user.

At the moment, I use the find('list')-method to retrieve the IDs and
store them in a variable. But I think if the groups grow, the PHP
cache won't handle this amount of data in a single variable.

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

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

发布评论

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

评论(1

灼痛 2024-08-14 05:54:00

您应该研究 Cake Has-And-Belongs-To-Many (HABTM) 关系。检查蛋糕书。它将允许您使用连接表在两个表之间创建关系,并将根据应用程序中的请求自动检索和保存值。

但请注意,在一个模型中,您无法(默认情况下)按相关模型(在本例中为连接表相关的模型)上的条件进行筛选。为此,您需要使用 Containable 行为,这将允许您在相关表上设置过滤条件。

另一个需要注意的是,我不知道一种方法(开箱即用)在连接表中添加一些有关关系的信息。例如,如果您想(在连接表中)记录用户是否完成了任务,则必须在那里编写自己的内容。我通常通过为连接表创建一个模型来解决这个问题,然后每当我想要检索特定于连接关系的数据时就调用该模型。通过这样做,您还可以轻松地从连接表中提取数据。还有其他人有更好的解决方案吗?

You should look into the Cake Has-And-Belongs-To-Many (HABTM) relationships. Check the cake book. It will allow you to create a relationship between two tables, using a join table, and will automatically retrieve and save values as requested in your application.

Please note, however, that from one model you cannot (by default) filter by criteria on the related model (in this case, the model related by the join table). To do this, you will need to use the Containable behavior, which will allow you to set filter criteria on the related tables.

The one other caveat is that I don't know of a way (out of the box) to add some information about the relationship in the join table. For example, if you wanted to record (in the join table) whether the user has completed the task, you would have to write your own stuff there. I usually get around this by creating a model for the join table, which I then invoke whenever I want to retrieve data specific to the joined relationship. By doing it this way, you can also easily pull up the data from the joined tables. Anybody else have a better solution?

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