Cakephp 有很多,属于需要帮助的协会
我不知道如何继续我的数据库模式和模型关联,任何帮助将不胜感激!
奥凯,就这样;
模型 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现这一点的最简单方法是使用查找表或连接表。 Cake 对于这些情况使用所谓的“具有并属于多”(HABTM) 关系,这是一种“多对多”关系类型。
查看HABTM 的手动条目 来处理这种情况。该手册提到了您想要的类似设置。
在您的情况下,您将拥有
posts
和tags
表,它们由名为posts_tags
的查找表连接,其中包含post_id
和tag_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
andtags
tables, which are joined by a lookup table calledposts_tags
containingpost_id
andtag_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.