开发者

Handling `lastUpdated` in a grails domain

开发者 https://www.devze.com 2022-12-11 02:49 出处:网络
I want to use the lastUpdated field of a Grails domain to impleme开发者_如何学编程nt a \"what objects have recently changed\" view. However my domain has a has-many association which when something is

I want to use the lastUpdated field of a Grails domain to impleme开发者_如何学编程nt a "what objects have recently changed" view. However my domain has a has-many association which when something is added will cause the lastUpdated value to be updated.

E.g.:

class Author {
    string name
    Date lastUpdated
    static hasMany = [ books : Book ]
}

Adding a new book (author.addToBooks(newBook)) will update lastUpdated.

Questions:

  1. Why is lastUpdated not updated when a book is removed?
  2. Is there a way to prevent lastUpdated to be changed when a book is added to the list? (Let's say that a new book does not count as a change to the author in my domain model)

Thanks!


Number 1 sounds like a bug (if it's updated when you add a book it should be consistent and update it when you remove one).

Number 2 - instead of using addToBooks, have you tried new Book(author:a).save() ? You might have to refresh the Author if you need to list their books in the same action but it should stop an update firing on the author


To get finer grained control over the date changes, you can add a "beforeUpdate" event to your domain class (see grails.org/GORM+-+Events). It looks like this is intended behavior though: http://jira.codehaus.org/browse/GRAILS-1857

0

精彩评论

暂无评论...
验证码 换一张
取 消