[英]Cakephp hasmany, belongsto associations help needed
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; Okei,就这样;
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) 模型1:发布(标识,标题,category_id,tag_id,主体,创建,修改)模型2:分类(标识,标题,描述,创建,修改)模型3:标签(标识,标题,描述,创建,修改)
Mu problem is how to "build" the database table to allow for multiple tags and categories, with associations. Mu问题是如何“构建”数据库表以允许具有关联的多个标签和类别。
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 示例:如果帖子1属于类别ID = 1,并且hasMany标记,tag_id:1、3、8、99示例2:如果帖子2属于类别ID = 1,5,并且hasMany标记,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
汤姆
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.
在这些情况下,Cake使用所谓的“具有很多”关系(HABTM),这是一种“多对多”的关系类型。
Have a look at the manual entry for HABTM to deal with this kind of scenario. 查看用于处理这种情况的HABTM的手动输入 。 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. 在您的情况下,您将拥有
posts
和tags
表,它们由一个名为posts_tags
的查找表(包含post_id
和tag_id
列)连接tag_id
。 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. 如果您在Post Model中正确定义了HABTM关系,Cake将知道在保存,查找和更新数据时如何正确使用这三个表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.