phpcake HABTM用户有很多朋友,表命名约定
我正在建模一个社交网络风格的网站,人们可以在其联系人列表中包含其他人,我想将这种关系建模为同一表用户上的 HABTM 关系,但根据 http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM我必须像这样创建表:
CREATE TABLE IF NOT EXISTS `users_users` (
`users_users.id` int(11) NOT NULL AUTO_INCREMENT,
`users_users.user_id` int(11) NOT NULL ,
`users_users.user_id` int(11) NOT NULL ,
PRIMARY KEY (`users_users.id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9
请帮助,我怎样才能避免重复?
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 User, but according to http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM i have to give create table like this:
CREATE TABLE IF NOT EXISTS `users_users` (
`users_users.id` int(11) NOT NULL AUTO_INCREMENT,
`users_users.user_id` int(11) NOT NULL ,
`users_users.user_id` int(11) NOT NULL ,
PRIMARY KEY (`users_users.id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9
Please Help, how can I avoid repetition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于引用同一表的 HABTM 关系,您需要更改外键的名称。定义
users_users
,包含三个字段:id
、user_id
和friend_id
。在您的 User 模型中,设置关系,分配显式的foreignKey和associationForeignKey:
For HABTM relationships that reference the same table, you need to change the name of the foreign key. Define
users_users
, with three fields:id
,user_id
, andfriend_id
.In your User model, set up the relationship, assigning an explicit foreignKey and associationForeignKey: