我在 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