2016-01-20 60 views
0

我的員工域具有相關的部門,部門和職位,這些部門本身都是域。這種關係被編碼爲嘗試創建條件的方法缺失

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

(注意,沒有belongsTohasOne關係)我在每一個這些從下拉列表中選擇的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: [[email protected]] 

我試圖把在該司/部門/位置eq片段的和塊內,但失敗,出現錯誤

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

任何想法我做錯了嗎?

回答

1

您需要在與.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 
    } 
}) 

另外請注意,您所使用的GString的語法,而無需在eq標準的必要"

相關問題