如何在Grails中建模朋友友谊关系
您将如何在Grails中建立朋友友谊关系模型?直到现在,我的用户类有很多追随者如何在Grails中建模朋友友谊关系,grails,social-networking,gorm,Grails,Social Networking,Gorm,您将如何在Grails中建立朋友友谊关系模型?直到现在,我的用户类有很多追随者 class User { //Searchable plugin static searchable = true String userId String password boolean enabled = true // For Spring Security plugin's user registration. String email String userRealName boolean email
class User {
//Searchable plugin
static searchable = true
String userId
String password
boolean enabled = true
// For Spring Security plugin's user registration.
String email
String userRealName
boolean emailShow
Date dateCreated
Profile profile
static hasMany = [
posts : Post,
tags : Tag,
following : User,
authorities : Role,
notifications: Notification,
locations: Location,
incomingLocations:IncomingLocation,
]
static belongsTo = Role
static constraints = {
userId(blank: false, size:3..20, unique: true)
password(blank: false)
dateCreated()
profile(nullable: true)
userRealName(nullable: true, blank: true)
email(nullable: true, blank: true)
}
static mapping = {
profile lazy:false
}
}
但是我想更改以下内容:User for friendships:friends并创建一个如下的friendships类:
class Friendship {
static belongsTo= [user:User]
User friend2
boolean areFriends
}
这是一个理想的实现吗
您将如何实现握手(接受/拒绝待定的友谊)?您可能不需要直接为友谊建模。你可以建立一个hasMany关系,将用户作为朋友联系起来。除非有人接受你的请求,否则你不会建立这种关系。如果他们不再想成为朋友,那么就删除这两个用户之间的关系
class User {
static hasMany = [friends:User]
}
class FriendRequest {
User fromUser
User toUser
}
这样友谊就不需要做两件事(联系用户和跟踪状态)。朋友成为了一种自然的对象关系,这可以使优化抓取之类的事情变得更容易。您可能不需要直接为友谊建模。你可以建立一个hasMany关系,将用户作为朋友联系起来。除非有人接受你的请求,否则你不会建立这种关系。如果他们不再想成为朋友,那么就删除这两个用户之间的关系
class User {
static hasMany = [friends:User]
}
class FriendRequest {
User fromUser
User toUser
}
这样友谊就不需要做两件事(联系用户和跟踪状态)。朋友成为了一种自然的对象关系,这可以使优化抓取之类的事情变得更容易