簡體   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