0

您将如何在 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
}

}

但我想更改以下内容:User for something like friends:Friendship 并创建一个 Friendship 类,如下所示:

class Friendship {

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

}

这是一个理想的实现吗?

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

4

1 回答 1

3

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

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

class FriendRequest {
    User fromUser
    User toUser
}

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

于 2009-07-11T20:08:19.470 回答