开发者

Mixed relationship in grails application

I have a domain class which is called Ev开发者_开发知识库ent. Event has many registrations, which are users with account on site or people without it. I want registered user to be able to track their events, so Event-User relationship should be bidirectional (user is created by Spring Security). Still there are registrations which are not connected with user accounts. It's important since event is kind of a live competition with ranking, which is updated by event owner when event is over. How to achieve this?


this should work:

class User {
    static hasMany = [events:Event]
}


class Event {
    User user
    static constraints = {
       user(nullable:true)
    }
}

this way you can get the user from the event or all events for the user.

EDIT:

class User {
    static hasMany = [events:Event]
}


class Event {
    static hasMany = [users:User]
}

add Users to the Event with

event.addToUsers(user)

remove Users from Event with

event.removeFromUsers(user)

get all Users from Event

event.users

I'm not quite sure how you handle the not registered Users. Do you have no information about this users at all (no username etc ?) I would suggest you add a flag in the User class "Boolean registered". So you can create a User object for not registered Users also.

cheers manu

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜