1

我在 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 上的定价配置文件视图中使用多项选择

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

参数:

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
4

2 回答 2

0

看起来您首先需要Store为该User.defaultStore属性创建一个,然后再尝试将所有内容绑定到User

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

  1. defaultStore.从拥有它的参数中删除。
  2. 像这样创建对象:

    Store defaultStore = new Store(params)
    User user = new User(params)
    user.defaultStore = defaultStore
    
于 2012-10-31T19:39:43.167 回答
0

我的解决方案是手动添加定价配置文件。

<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)
}
于 2012-11-14T15:49:09.853 回答