您将如何塑造 friend -Grails中的友谊关系?到目前为止,我的User类有很多关注者
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
}
}
但我想更改以下内容:用户,例如友谊:Friendship,并创建如下的Friendship类:
class Friendship {
static belongsTo= [user:User]
User friend2
boolean areFriends
}
这是理想的实现吗?
您将如何实现握手(接受/拒绝待定的友谊)?
最佳答案
您可能不需要直接为友谊建模。您可以拥有一个hasMany关系,将用户关联为 friend 。除非有人接受FriendRequest,否则您不会创建该关系。如果他们不再想成为 friend ,则只需删除2个用户之间的关系。
class User {
static hasMany = [friends:User]
}
class FriendRequest {
User fromUser
User toUser
}
这样,友谊就不必做两件事(关联用户和跟踪状态)。 friend 变成了自然的对象关系,可以使某些事情(例如优化获取)变得更加容易。
关于grails - 如何在Grails中建立“ friend -友谊”关系模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1114509/