2009-07-11 84 views
0

您将如何建模Grails中的朋友关系?到现在为止我的User类有许多追随者如何建模朋友 - 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 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 
} 

}

但我想改变如下:用户对于像友谊:友谊,创造一个友好的类如下:

class Friendship { 

static belongsTo= [user:User] 
User friend2 
boolean areFriends 

}

这是一个理想的实现?

您将如何实施握手(接受/拒绝待定的友谊)?

回答

3

您可能不需要直接建模友谊。你可以拥有一个将用户作为朋友的hasMany关系。在有人接受FriendRequest之前,您不会创建该关系。如果他们不再想成为朋友,那么只需删除2个用户之间的关系即可。

class User { 
    static hasMany = [friends:User] 
} 

class FriendRequest { 
    User fromUser 
    User toUser 
} 

这样友谊不必做2件事(关联用户和跟踪状态)。而朋友变成了一种自然的对象关系,可以使一些事情比如优化获取更容易一些。