Grails Multiple Select for One-to-many

时间:2012-10-31 16:25:42

标签: grails grails-2.0

我在Grails 2.0.4中 我收到了错误:

没有方法签名:com.example.User.addToDefaultStorePricingProfiles()适用于参数类型:(com.example.PricingProfile)值:bindData()行上的[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" />

存储控制器

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])
    }
}

PARAMS:

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

2 个答案:

答案 0 :(得分:0)

在尝试将所有内容绑定到Store

之前,您首先需要为User.defaultStore属性创建User

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

  1. 从拥有它的参数中删除defaultStore.
  2. 创建如下对象:

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

答案 1 :(得分:0)

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

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