Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No owner defined between domain classes: many to many relationship

These are my domain classes:

class Game {
    static hasMany = [players: User]
    static belongsTo = [owner: User]
}

class User {
    static hasMany = [games: Game]
}

If I try to use them as they are I get No owner defined between domain classes. So I need to set the owner of the relationship. Adding static belongsTo = Game to User causes Domain classes cannot own each other in a many-to-many relationship.

The only other option I can think of is to add static belongsTo = User to the Game class but I already have a belongsTo there.

How do I model this?

like image 600
zoran119 Avatar asked Jun 03 '13 10:06

zoran119


2 Answers

class Game {
    User owner
    static hasMany = [players: User]
    static belongsTo = User
}

class User {
    static hasMany = [games: Game]
}

You will have to specify one side of the relationship, as the owner, by doing this you will make User domain class as the owner of the many to many relationship.

The belongsTo field controls where the dynamic addTo*() methods can be used from. we’re able to call User.addToGames() because Game belongsTo User. we can’t call Game.addToPlayers().

like image 179
Saurabh Dixit Avatar answered Nov 05 '22 17:11

Saurabh Dixit


Try this:

class Game {
    User owner
    static hasMany = [players: User]
    static belongsTo = User
}
like image 1
James Kleeh Avatar answered Nov 05 '22 15:11

James Kleeh