0
   def names = domain.dirtyPropertyNames
    for (name in names) {
        def originalValue = domain.getPersistentValue(name)
        def newValue = domain."$name"
    }

但是如果我与其他域有 1-1 的关系

我如何访问该其他域的dirtyPropertyNames

def dirtyProperties = domain?.otherDomain?.dirtyPropertyNames
    for (name in dirtyProperties ) {
        def originalValue = domain?.otherDomain?.getPersistentValue(name)
        def newValue = domain?.otherDomain?."$name"
    }

但我得到没有这样的属性:dirtyPropertyNames for class: otherDomain

4

1 回答 1

1

在针对 Grails 2.2.4 和 2.3.0 进行测试时,这似乎不是问题。
您是如何调整 1 对 1 关系的?

这是一个示例,希望对您有所帮助:

class Book {
    String name
    String isbn
    static hasOne = [author: Author]
}

class Author {
    String name
    String email
    Book book
}

//Save new data
def book = new Book(name: 'Programming Grails', isbn: '123')
book.author = new Author(name: "Burt", email: 'test', book: book)
book.save(flush: true)
//Sanity check
println Book.all
println Author.all

//Check dirty properties of association
def book = Book.get(1)
book.author.name = 'Graeme'

def dirtyProperties = book?.author?.dirtyPropertyNames
for (name in dirtyProperties ) {
    println book?.author?.getPersistentValue(name) //Burt
    println book?.author?."$name" //Graeme
}

虽然,在 Grails 2.3.0 中,您可以保持 1-1 关系,如下所示:

def author = new Author(name: "Burt", email: 'test')
def book = new Book(author: author, name: 'PG', isbn: '123').save(flush: true)
于 2013-09-14T01:15:08.753 回答