CakePHP 中同一张表上的 HABTM 关系
我正在建模一个社交网络风格的网站,人们可以在其联系人列表中包含其他人,我想将这种关系建模为同一张表(即用户)上的 HABTM 关系,这是一个好主意吗? 或者有更好的方法吗?
I was modeling a social networking style website where people would be able to have other people on their contacts list, i wanted to model this relationship as a HABTM relation on the Same table i.e User, is this a good idea? or is there a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于。 如果您只想在用户之间创建关系,那么您可以简单地使用 HABTM。 如果您想存储有关关系本身的更多信息,那么您可能需要将其设为一个单独的对象。 例如,也许您想存储两个用户之间的关系类型。 朋友? 同事? 合作伙伴?
在这种情况下,您可以创建单独的关系或联系人对象。 然后,用户有许多联系人,每个联系人属于两个用户。 这与手动创建 HABTM 关系基本相同,但由于该关系本身现在是一个单独的模型(即联系人),因此您可以在其中存储额外的信息。
It depends. If all you want to do is create relationships between users then you can simply use a HABTM. If you want to store more information about the relationship itself then you may want to make it a separate object. For example, perhaps you want to store what kind of relationship two users have. Friend? Co-worker? Parters?
In that case you could create a separate Relationship or Contact object. Then, User hasMany Contacts and every Contact belongsTo two Users. This is basically the same as manually creating a HABTM relationship, but because the relationship itself is now a separate Model (i.e. Contact) you can store extra information in it.