Cakephp 有很多,属于需要帮助的协会

发布于 2025-01-02 09:14:30 字数 401 浏览 3 评论 0原文

我不知道如何继续我的数据库模式和模型关联,任何帮助将不胜感激!

奥凯,就这样;

模型 1:柱状 (id、标题、category_id、tag_id、正文、创建、修改) 模型2:类别 (id、标题、描述、创建、修改) 模型 3:标签 (id、标题、描述、创建、修改)

Mu 问题是如何“构建”数据库表以允许多个标签和类别,并具有关联性。

示例:如果帖子 1 属于category_id = 1,并且有多个标签,则 tag_id: 1, 3, 8, 99 示例2:如果帖子 2 属于category_id = 1,5 且 hasMany Tag, tag_id: 1, 8, 43

所以基本上,我不确定要使用什么模式以及允许多个关联的最佳实践。

有什么建议吗??

谢谢! -汤姆

I am not sure how to proceed for my database schema and for my model associations, any help will be appreciated!!!

Okei, here it goes;

Model 1 : Post
(id, title, category_id, tag_id, body, created, modified)
Model 2 : Category
(id, title, description, created, modified)
Model 3 : Tag
(id, title, description, created, modified)

Mu problem is how to "build" the database table to allow for multiple tags and categories, with associations.

Example: If Post 1 belongsTo category_id = 1, and hasMany Tag, tag_id: 1, 3, 8, 99
Example2: If Post 2 belongTo category_id = 1,5 and hasMany Tag, tag_id: 1, 8, 43

So basicly, I am not sure what schema to go with and the best practice to allow for multiple associations.

Any suggestions??

Thanks!
-Tom

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

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

发布评论

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

评论(1

陪我终i 2025-01-09 09:14:30

实现这一点的最简单方法是使用查找表或连接表。 Cake 对于这些情况使用所谓的“具有并属于多”(HABTM) 关系,这是一种“多对多”关系类型。

查看HABTM 的手动条目 来处理这种情况。该手册提到了您想要的类似设置。

在您的情况下,您将拥有 poststags 表,它们由名为 posts_tags 的查找表连接,其中包含 post_idtag_id 列。如果您在 Post Model 中正确定义了 HABTM 关系,Cake 将知道在保存、查找和更新数据时如何正确使用这三个表。

The easiest way to implement this is to use a lookup or join table. Cake uses a so-called "Has And Belongs To Many" (HABTM) relationship for these cases, which is a "many to many" relationship type.

Have a look at the manual entry for HABTM to deal with this kind of scenario. The manual mentions a comparable setup you're aiming for.

In your case you would have posts and tags tables, which are joined by a lookup table called posts_tags containing post_id and tag_id columns. If you define the HABTM relation correctly in your Post Model, Cake will know how to use the three tables correctly when saving, finding and updating data.

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