Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails - how execute code before every save?

Is there a good/standard way to execute some common code before every save() invocation on domain classes?

For example, my domain

class Page {

    String url
    Boolean processed
    Date date
    Integer urlCrc 
}

My form has only 3 first fields and I would like to calculate urlCrc every time the save() method is called. I cannot just override save method because it is injected.

like image 868
mrok Avatar asked Jun 03 '12 20:06

mrok


1 Answers

You can use GORM events - see the docs. Since by default validate() is called before every save() I would use that.

class Page {
    //your defs here

    def beforeValidate() {
        this.urlCrc = yourComputationHere
    }
}
like image 84
Kelly Avatar answered Nov 11 '22 03:11

Kelly