Grails一对多选择

Grails一对多选择,grails,grails-2.0,Grails,Grails 2.0,我在Grails2.0.4中 我得到了一个错误: 方法com.example.User.addToDefaultStorePricingProfiles()的签名不适用于bindData()行上的参数类型:(com.example.PricingProfile)值:[com.example.PricingProfile:5]。在添加已保存的PricingProfiles之前,我是否必须先保存用户和存储,或者有更好的方法来执行此操作 模型 class User { transient s

我在Grails2.0.4中 我得到了一个错误:

方法com.example.User.addToDefaultStorePricingProfiles()的签名不适用于bindData()行上的参数类型:(com.example.PricingProfile)值:[com.example.PricingProfile:5]。在添加已保存的PricingProfiles之前,我是否必须先保存用户和存储,或者有更好的方法来执行此操作

模型

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]
}
我在商店的pricingProfiles视图中使用了多选

<g:select from="${retailer.pricingProfiles}" name="defaultStore.pricingProfiles" value="${user?.defaultStore?.pricingProfiles*.id}" multiple="multiple" optionKey="id" optionValue="name" class="pricingProfiles" />
参数:

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

在尝试将所有内容绑定到
用户之前,似乎首先需要为
用户.defaultStore
属性创建
存储

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

  • 从包含它的参数中删除
    defaultStore.
  • 创建如下所示的对象:

    Store defaultStore = new Store(params)
    User user = new User(params)
    user.defaultStore = defaultStore
    

  • 我的解决方案是手动添加pricingProfiles

    <g:select from="${retailer.pricingProfiles}" name="pricingProfiles" value="${user?.defaultStore?.pricingProfiles*.id}" multiple="multiple" optionKey="id" optionValue="name" class="pricingProfiles" />
    

    您的用户域是什么样子的?问题在于
    用户
    ,您可能在gsp的某个地方有一个输入,或者很可能是一个名称与用户类的属性重叠的select。您知道要发送的参数是什么吗?您可以粘贴用户类吗?
    <g:select from="${retailer.pricingProfiles}" name="pricingProfiles" value="${user?.defaultStore?.pricingProfiles*.id}" multiple="multiple" optionKey="id" optionValue="name" class="pricingProfiles" />
    
    params.pricingProfiles.each {
        PricingProfile pricingProfile = PricingProfile.get(it)
        user.defaultStore.addToPricingProfiles(pricingProfile)
    }