Search code examples
grailsgrails-ormgrails-2.0

Can I Determine When a Domain Class is Added?


If I have 2 (or more) domain classes with a hasMany of C, how can I determine if an entity of C has been added?

class A {
    String name
    static hasMany = [cs: C]
class B {
    String name
    static hasMany = [cs: C]
}
class C {
    String someProperty
}

// In CController add Action
//...
genericInstance.addToCs(cInstance)

I'm looking for more of an event I can handle. CController is used for inline forms for A and B, but I need two different processes to run depending on which domain C is added to


Solution

  • You have the possibility to write your own addToCs method / Closure in your classes A and B.

    class A {
        String name
        static hasMany = [cs: C]
        def addToCs = {
            // Do what you want with your value
            cs.add(C)
        }
    }
    

    Be aware of what happens in default addToCs : DomainClassGrailsPlugin.groovy:289 @github