gpt4 book ai didi

grails - 向我的 Controller 列表方法添加更多约束

转载 作者:行者123 更新时间:2023-12-02 15:31:35 26 4
gpt4 key购买 nike

目前我的域对象产品的 Controller 列表方法是:

def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
[productInstanceList: Product.list(params), productInstanceTotal: Product.count()]
}

我希望为此添加更多约束。更具体地说,我想获取登录用户。这与称为 Manager 的域实体具有 M:1 关系。 Manager 与域实体 Company 具有 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,我只想加入以表示所有用户经理的公司的产品,然后将其作为 where 放入。谓词并命中产品表。

但我想要一个 grails/groovy 解决方案。我还需要保留 params在查询中,因为这个 Controller 也可以有 params注入(inject)到查询中,

有小费吗?

最佳答案

您可以使用createCriteria得到所有的产品,比如

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]
}

关于grails - 向我的 Controller 列表方法添加更多约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18553566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com