mjswartz
mjswartz

Reputation: 743

Missing methods trying to create a criteria

My Employee domain has an associated division, department, and position, which are all domains themselves. This relationship is coded as

class Employee
{
    ...
    String firstName
    String lastName
    Position position
    Division division
    Department department
    ...
}

(note that there is no belongsTo or hasOne relationship) I send in the id of each of these selected from a drop-down list with

<g:select name="department" from="${Department.list()}" optionKey="id" optionValue="${{it.name}}" />

(similarly for division and position), but I am getting an error with the code querying the database

def employeeList = Employee.createCriteria().list(sort: params.sort, order: params.order) 
{
    and 
    {
        ilike "firstName", "%${params.firstName}%" 
        ilike "lastName", "%${params.lastName}%"
    }
    position
    {
        eq "position", ${params.position}
    }
    department
    {
        eq "department", ${params.department}
    }
    division
    {
        eq "division", ${params.division}
    }
}

The error I get is

No signature of method: EmployeeController.department() is applicable for argument types: (EmployeeController$_results_closure1_closure4) values: [EmployeeController$_results_closure1_closure4@f559db0]

I've tried putting in the division/department/position eq snippets inside the and block, but that fails with the error

No signature of method: EmployeeController.and() is applicable for argument types: (EmployeeController$_results_closure1_closure4) values: [EmployeeController$_results_closure1_closure4@2f3302f3]

Any ideas what I'm doing wrong?

Upvotes: 0

Views: 44

Answers (1)

doelleri
doelleri

Reputation: 19682

You need to start your criteria closure on the same line as the .list() call. The closure is supposed to be an argument to the list() method but when it starts on the next line Groovy is executing list() and then trying to create a closure.

def employeeList = Employee.createCriteria().list(sort: params.sort, order: params.order) {
    and {
        ilike "firstName", "%${params.firstName}%" 
        ilike "lastName", "%${params.lastName}%"
    }
    position {
        eq "position", params.position
    }
    department {
        eq "department", params.department
    }
    division {
        eq "division", params.division
    }
}

This is equivalent to below (note the parenthesis)

def employeeList = Employee.createCriteria().list([sort: params.sort, order: params.order], {
    and {
        ilike "firstName", "%${params.firstName}%" 
        ilike "lastName", "%${params.lastName}%"
    }
    position {
        eq "position", params.position
    }
    department {
        eq "department", params.department
    }
    division {
        eq "division", params.division
    }
})

Also note that you were using GString syntax without the requisite " in your eq criteria.

Upvotes: 1

Related Questions