我用ItemController.groovy编写了下面这段代码
def list = {
params.max = 60
def storeYYId = params.id
[itemInstanceList: Item.list(params), itemInstanceTotal: Item.count()]
}我在Item.groovy中有以下内容:
class Item {
String itemName
static belongsTo = [store:Store]
static constraints = {
itemName(blank:false)
storeId()
}
}这给了我一个错误,因为它告诉我没有storeId属性,但有,因为store_id是相应数据库中Store表的外键。
Question1。我如何告诉grails让我访问由GORM自动生成的域的属性,比如本例中的id和storeId?
Question2。为了仅检索storeId == storeYYId所在位置的项目列表,我应该在列表操作的ItemController.groovy中编写什么代码?
发布于 2011-11-10 20:04:25
Question1.我如何告诉grails让我访问由GORM自动生成的域的属性,比如本例中的id和storeId?
您应该能够以与访问您定义的属性完全相同的方式访问自动生成的属性。出现错误的原因是因为Grails不会自动为Item类生成storeId属性,它将自动生成的属性只有version和id (对于Item和Store)。
Question2.为了仅检索storeId == storeYYId所在位置的项目列表,我应该在列表操作的ItemController.groovy中编写什么代码?
您需要编写HQL或criteria查询来检索这些项。criteria查询将如下所示(未测试)
// Get all items that have storeId = 6
def storeId = 6
def items = Item.withCriteria {
store {
eq('id', storeId)
}
}https://stackoverflow.com/questions/8079053
复制相似问题