I've a Grails 2.2.1 project having a domain named 'Question'. Within this domain, beforeinsert() currently look like this:
def beforeInsert() {
dateCreated = new Date()
dateModified = new Date()
}
I want to update dateCreated if this value is passed from a controller and if not, allow as it is. Here's my modification, but unfortunately its not working:
def beforeInsert() {
dateCreated = dateCreated ? dateCreated : new Date()
println dateCreated
dateModified = new Date()
}
Even though println prints updated dateCreated value, when data inserted to database, dateCreated field gets the value of current timestamp every time. Any help appreciated :)
The reason why dateCreated
is being automatically set for you is because of the default behavior of autoTimestamp
. By default, when a property dateCreated
exists on a domain class Grails will set this value for you.
You can disable this default behavior as such:
class Book {
…
static mapping = {
autoTimestamp false
}
}
You may want to check out the documentation as well.