Grails 向“我的控制器列表”方法添加更多约束

Grails 向“我的控制器列表”方法添加更多约束,grails,Grails,目前,域对象产品的控制器列表方法是: def list(Integer max) { params.max = Math.min(max ?: 10, 100) [productInstanceList: Product.list(params), productInstanceTotal: Product.count()] } 我希望在这方面增加更多限制。更具体地说,我想获得登录用户。这与名为Manager的域实体具有M:1关系。经理与域实体公司有M:M关系。公司与产品有1:

目前,域对象产品的控制器列表方法是:

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    [productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
}
我希望在这方面增加更多限制。更具体地说,我想获得登录用户。这与名为Manager的域实体具有M:1关系。经理与域实体公司有M:M关系。公司与产品有1:M的关系。因此,我只想退回适当公司的产品

我首先要做:

def list(Integer max) {
// 1. Get logged in user. 
def user = springSecurityService.currentUser;
    // 2. Get corresponding manager.
    Manager mgr = user.getManager();
    // 3. Get corresponding companies
    companies = mgr.getCompanies();

    // 4. Get products for all companies - not sure smart way to get this.
    // 5. Need to add the products to the query
    ...
}
如图所示,我在第4步、第5步遇到了困难。使用原始SQL,我只需要一个连接来表示用户经理公司的所有产品,然后将其作为
谓词放入其中,并点击产品表

但是我想要一个grails/groovy解决方案。我还需要在查询中保留
参数
,因为此控制器也可以将
参数
注入查询中

有什么建议吗?

你可以用它来获得所有的产品,比如

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    def user = springSecurityService.currentUser;
    Manager mgr = user.getManager();
    companies = mgr.getCompanies();
    def productList = Product.createCriteria().list(params) {
        'in'("companies", companies)
    }
    [productInstanceList: productList, productInstanceTotal: productList.totalCount]
}