grails域为子对象的dirtyPropertyNames

时间:2013-09-13 20:23:39

标签: grails groovy

   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:otherDomain

1 个答案:

答案 0 :(得分: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)