用于一对多的 Grails 多选

标签 grails grails-2.0

我在 Grails 2.0.4
我收到错误:

没有方法签名:com.example.User.addToDefaultStorePricingProfiles() 适用于参数类型:(com.example.PricingProfile) 值:bindData() 行上的 [com.example.PricingProfile : 5]。在添加已经持久化的定价配置文件之前,我是否必须先保存用户和存储,还是有更好的方法来做到这一点?

楷模

class User {

    transient springSecurityService

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    Store defaultStore

    Date dateCreated
    Date lastUpdated

    static hasMany = [orders: Order]
}

class Store {

  String storeNumber
  String name
  PricingProfile defaultPricingProfile

  static belongsTo = [retailer: Retailer]

  static hasMany = [pricingProfiles: PricingProfile]
}

class PricingProfile {

  String name

  static belongsTo = [retailer: Retailer]
}

我在 Store 上的定价配置文件 View 中使用多项选择
<g:select from="${retailer.pricingProfiles}" name="defaultStore.pricingProfiles" value="${user?.defaultStore?.pricingProfiles*.id}" multiple="multiple" optionKey="id" optionValue="name" class="pricingProfiles" />

存储 Controller
def save() {
    Retailer retailer = Retailer.get(params.retailer)
    User user = new User()
    user.defaultStore = new Store()

    bindData(user, params)

    user.validate()
    user.defaultStore.validate()

    if (user.hasErrors() || user.defaultStore.hasErrors()) {
        log.error("Error saving store: ${user.errors.fieldErrors} ${user.defaultStore.errors.fieldErrors}")
        flash.storeError = "Please correct the errors below"
        render(view: 'create')
    } else {
        retailer.addToStores(user.defaultStore)
        retailer.addToUsers(user)
        retailer.save(failOnError: true, flush: true)

        flash.confirm = "Store ${user.defaultStore.storeNumber} successfully added"
        redirect (action: 'list', params: [retailer: retailer.id])
    }
}

参数:
defaultStore.pricingProfiles: 2
defaultStore.pricingProfiles: 3
defaultStore.pricingProfiles: 4
defaultStore.defaultPricingProfile.id: 2
retailer: 2
submitStore: Save
defaultStore.storeNumber: 888
username: wert
password: wert
defaultStore.name: Fake Store

最佳答案

看来您首先需要创建一个 Store对于User.defaultStore在尝试将所有内容绑定(bind)到 User 之前的属性

Grails 似乎无法自动为您做到这一点。
鉴于 StoreUser具有不同名称的属性,您可以执行以下操作:

  • 删除 defaultStore.从拥有它的参数中。
  • 像这样创建对象:
    Store defaultStore = new Store(params)
    User user = new User(params)
    user.defaultStore = defaultStore
    
  • 关于用于一对多的 Grails 多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162487/

    相关文章:

    grails - grails 2中的PagedResultList问题

    Grails - 创建和映射 View 时出错

    grails - Grails 2.5在服务类上设置数据源不起作用

    grails - 从 grails 的 lib 文件夹中读取 jar 文件

    grails - Spock @Unroll 注释

    grails - 使用 grails 后处理 *.js 文件

    java - 如何移动列表条目并同时更新所有条目的索引?

    json - gzipping 来自 grails Controller 的 json 响应

    tomcat - 部署在 tomcat 服务器上的 Grails 应用程序的最佳 PermSize?

    grails - Grails日历插件问题-值字段中的字符串 'struct'