我的员工域具有关联的部门、部门和职位,它们本身都是域。此关系编码为
class Employee
{
...
String firstName
String lastName
Position position
Division division
Department department
...
}
(请注意,没有belongsTo
或hasOne
关系)我发送从下拉列表中选择的每个id,并带有
<g:select name="department" from="${Department.list()}" optionKey="id" optionValue="${{it.name}}" />
(除法和位置类似),但是我在查询数据库的代码时遇到错误
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}
}
}
我得到的错误是
No signature of method: EmployeeController.department() is applicable for argument types: (EmployeeController$_results_closure1_closure4) values: [EmployeeController$_results_closure1_closure4@f559db0]
我尝试在 and 块中放入部门/部门/职位eq
片段,但失败并出现错误
No signature of method: EmployeeController.and() is applicable for argument types: (EmployeeController$_results_closure1_closure4) values: [EmployeeController$_results_closure1_closure4@2f3302f3]
知道我做错了什么吗?
您需要在
与.list()
调用相同的行上开始条件闭包。闭包应该是list()
方法的参数,但是当它在下一行开始时,Groovy正在执行list()
,然后尝试创建闭包。
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
}
}
这相当于下面(注意括号)
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
}
})
另请注意,您在eq
条件中使用了 GString 语法,但没有必要"
。