Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 访问自动生成的字段和查询_Grails_Groovy - Fatal编程技术网

Grails 访问自动生成的字段和查询

Grails 访问自动生成的字段和查询,grails,groovy,Grails,Groovy,我在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

我在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表的外键

问题1。我如何告诉grails让我访问GORM自动生成的域的属性,比如本例中的id和storeId

问题2。在我的列表操作中,我应该在ItemController.groovy中编写什么代码,以便只检索storeId==storeYYId的项目列表

问题1。我如何告诉grails让我访问 GORM自动生成的域,如中的id和storeId 这个案子

您应该能够以与访问定义的属性完全相同的方式访问自动生成的属性。出现错误的原因是Grails不会自动为
类生成
存储id
属性,它将自动生成的唯一属性是
版本
id
(对于
存储

问题2。在我的应用程序中,我应该在ItemController.groovy中编写什么代码 列表操作,以便仅检索 storeId==storeYYId

您需要编写HQL或criteria查询来检索这些项。条件查询看起来像这样(未测试)

问题1。我如何告诉grails让我访问 GORM自动生成的域,如中的id和storeId 这个案子

您应该能够以与访问定义的属性完全相同的方式访问自动生成的属性。出现错误的原因是Grails不会自动为
类生成
存储id
属性,它将自动生成的唯一属性是
版本
id
(对于
存储

问题2。在我的应用程序中,我应该在ItemController.groovy中编写什么代码 列表操作,以便仅检索 storeId==storeYYId

您需要编写HQL或criteria查询来检索这些项。条件查询看起来像这样(未测试)


也可以简单地执行Item.findAllByStore(Store.get(storeId))@Gregg该解决方案肯定更简单,但我希望它会导致发出两个查询,这是我想要避免的。但是,get()总是尝试从缓存中提取。因此,打击可能微不足道。我想你也可以做Item.findAllByStore(Store.load(storeId)),这就足够了,甚至不需要去缓存。也可以简单地做Item.findAllByStore(Store.get(storeId))@Gregg这个解决方案肯定更简单,但我希望它会导致发出两个查询,这是我想要避免的。但是,get()总是尝试从缓存中提取。因此,打击可能微不足道。我想您也可以完成Item.findAllByStore(Store.load(storeId)),这就足够了,甚至不需要去缓存。
// Get all items that have storeId = 6
def storeId = 6

def items = Item.withCriteria {
    store {
        eq('id', storeId)
    }
}