繁体   English   中英

为子对象grails域dirtyPropertyNames

[英]grails domain dirtyPropertyNames for child object

   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"
    }

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

在针对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
}

虽然,在wrt 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM