2011-08-25 31 views
2

我正在嘗試使用動態查找器搜索statusOpenOn(date)兩個字段。此查詢工作狀態良好,罰款:在grails中使用動態查找器搜索值

render(view:'list', model:[incidentInstanceList:Incident.findAllByStatusIlikeAndOpenOnGreaterThan("closed",new Date()-1,[sort:"id",order:"desc"])]) 

但現在我想通過三個字段,即UserIdstatusOpenOn(date)與動態查找器來搜索:

render(view:'list', model:[incidentInstanceList:Incident.findAllByStatusIlikeuserIdIlikeAndOpenOnGreaterThan("closed","${session.user.userId}",new Date()-1,[sort:"id",order:"desc"])]) 

此查詢不正常工作並顯示一個錯誤:

No signature of method: app.Incident.findAllByStatusIlikeuserIdIlikeAndOpenOnGreaterThan() is applicable for argument types: (java.lang.String, org.codehaus.groovy.runtime.GStringImpl, java.util.Date, java.util.LinkedHashMap) values: [closed, tt10004, Wed Aug 24 15:12:21 IST 2011, [sort:id, order:desc]] Possible solutions: findAllByStatusIlikeuserIdIlikeAndCreatedOnGreaterThan(java.util.List) 

請指導我解決這個問題。

回答

6
def incidentInstanceList = { 

    def criteria = Incident.createCriteria() 

    def results = criteria { 
     and { 
      user { 
       like('userId', "${session.user.userId}").toString() 
      } 
      like('status', "Closed") 
      gt('closedOn',new Date()-1) 
      sort("id", "desc") 
     } 

    } 

    render(view:'list', model:[ incidentInstanceList: results, incidentInstanceTotal: Incident.count()])   
} 
4

目前,您只能使用動態查找器,最大值爲兩個條件。如果您需要使用更多,則應考慮使用CriteriaHQL

This answer對你的問題更詳細。

0

而且我覺得沒有必要把第二個參數「」只是不session.user.userId(我假設ü不`噸需要用戶id是字符串這裏)createCriteria是什麼ü需要:)

1

可能這樣的事情,這,順便說一句,似乎更清晰的對我來說:

def incidentInstanceList = 
    Incident.createCriteria().list 
    { 
     eq(status, 'closed') 
     ilike(userId, "${session.user.userId}") 
     gt(openOn, new Date()-1) 
     sort("id", "desc") 
    } 
+0

謝謝回覆 – manu