我在 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 似乎无法自动为您做到这一点。
鉴于 Store
和 User
具有不同名称的属性,您可以执行以下操作:
defaultStore.
从拥有它的参数中。 Store defaultStore = new Store(params)
User user = new User(params)
user.defaultStore = defaultStore
关于用于一对多的 Grails 多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162487/