使用belongsTo字段的标准返回空结果

时间:2014-02-05 13:20:15

标签: grails gorm

我有两个控制器:timesheetcontroller和顾问控制器。在时间表我有字段:

class Timesheet {

...
String orderNumber
String groupCode

static belongsTo = [consultant: Consultant] //here
...

在顾问中:

class Consultant {
...
    String userName
...

如何构建查询,该查询将按顾问名称过滤时间表列表?我正在尝试下面这样的事情,但它不起作用。

获得顾问名称:

<input type="text" id="consultant" name="consultant" value="${flash.consultant}"/>

在TimesheetController中:

def list = {
    flash.consultant = params.consultant //here
    flash.groupCode = params.groupCode
    flash.orderNumber = params.orderNumber

    if(!params.max) {
        params.max = 20
    }
    def query
    def criteria = Timesheet.createCriteria()
    def results

    query = {
        and{
            like("userName", '%' + params.consultant + '%') //here
            like("groupCode", '%' + params.groupCode + '%')
            like("orderNumber", '%' + params.orderNumber + '%')
        }
    }

    results = criteria.list(params, query) 

    render(view:'index', model:[ timesheetInstanceList: results ])

}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

编辑: 您可以使用findAll方法并在HQL中提供给顾问。

results = Timesheet.findAll("from Timesheet timesheet where timesheet.consultant.userName like :userName and timesheet.orderNumber like :orderNumber and timesheet.groupCode:groupCode",
                [userName: "%${params.consultant}%",orderNumber:"%${params.orderNumber}%",groupCode:"%${params.groupCode}%"],
                [max: params.max,offset:params.offset])