Grails domain obj action argument and association data binding

一笑奈何 提交于 2019-12-23 12:32:33

问题


I know Grails v2.3 introduced some changes to the way data binding works, but I'm struggling to figure out how to get automatic association binding to work when I have an domain obj as the action argument. For instance, with a couple of simple domain objects:

class Author {
    String name
    List books
    static hasMany = [books: Book]
}

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

and controller action:

def doSomething(Author a) {
    // only simple properties appear to be bound at this point
}

if the incoming param obj is [id:4, name:Stephen King], the Author obj with ID 4 will be loaded and the name set to 'Stephen King'. If, however, I try to set one book reference, generating a param obj that looks like this: [id:4, name:Stephen King, books[0].id:21, books[0]:[id:21]], only the name change is bound; the books association remains unchanged. (It's also worth noting that a.hasErrors() == false as well.)

If, however, I adjust the controller action to be like this:

def doSomething(Author a) {
    a.properties = params
    // now the books association is bound
}

the data binding works as expected...but that kind of defeats the purpose of using an action argument.

What am I doing wrong?

来源:https://stackoverflow.com/questions/20414518/grails-domain-obj-action-argument-and-association-data-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!