I added "dateCreated" and "lastUpdated" Date properties to a domain class and then got a PropertyValueException after trying to save it. Further, one can't pass in values in the constructor because apparently these properties are now no longer bindable:
So, IIUC, one must instantiate a new domain object and then explicitly set these properties before saving:
myObj = new MyObj(someProp: 'foo')
myObj.dateCreated = new Date()
myObj.lastUpdated = new Date()
myObj.save()
Clearly, I don't want to have to remember to do that all the time. A no-args constructor would seem to be the next obvious choice:
MyObj() { dateCreated = new Date(); lastUpdated = new Date() }
But then I expect many unnecessary calls to Date as they'll be overwritten by incoming values from the db; plus, I'll need to remember to add such a no-args constructor to each domain class. What am I doing wrong?
I would have expected autoTimestamp to automatically timestamp my domain objects.
The documentation I've read:
However, now that I read more of that last reference, I see that the recommendation might be to set static NULL_DATE and then add a "beforeInsert" event that sets the properties if they've not already been set. Unfortunately, that's even more boilerplate to remember though it is an improvement over the no-args constructor as it avoids unnecessarily calling Date.