Fabien Barbier
Fabien Barbier

Reputation: 1524

Grails GORM on multiple Forms

Usually I'm using One-to-many relationship by this way :

class Study {
    static hasMany = [ crfs : Crf ]
    String name 
    ...
} 

class Crf { 
String title
String info 
...
} 

I can extend this relationship to others domains, Ex :

static hasMany = [ crfs : Crf, crfb : CrfBlood ...]

But in my case I have to link the Study domain to 30 others domains, maybe more...(ex : CrfBlood, CrfMedical, crfFamily, etc...).

What domain model implementation should I use in my case ?
I would like to keep the dynamic finders usability in my project.

Update - model complement :

A Study can have one-to-many Subject.
A Study can have one-to-many Crfs (ex : CrfBlood, CrfMedical, crfFamily, etc...).
A Subject can have one-to-many Visit (ex : a subject can have several Blood testing).

I would like to dynamically assign Crfs to a Study, so how can I use GORM (dynamic finders) without using static hasMany = [...] in my domain ?
Maybe, I can implement a service to do the same stuff did by hasMany ?

Upvotes: 3

Views: 366

Answers (1)

Gustavo Giráldez
Gustavo Giráldez

Reputation: 2690

You can declare all Crf types as subclasses of Crf, so that you'll only have one relationship to Study, but still be able to add the different types.

class Crf {
    String title
    String info
}

class CrfBlood extends Crf {
    String detailBlood
}

class CrfMedical extends Crf {
    String detailMedical
}

class Study {
    String name
    static hasMany = [ crfs: Crf ]
}

Then you can do:

def s = new Study(...)
def c1 = new CrfBlood(...)
def c2 = new CrfMedical(...)
s.addToCrfs(c1)
s.addToCrfs(c2)

Upvotes: 0

Related Questions