开发者

Which is best way to search for multiple values in multiple tables in grails? Plugin or dynamic finders?

I'm using dynamic finders to search, but when I search for fields in other tables there is an error. This is my action

def searchResults ={
 if (request.method == 'POST') {
 def criteria = Incident.createCriteria()

 def results = criteria {
 and {
   like('status', '%' + params.status + '%')
   like('severity', '%' + params.severity + '%')
   assignmentGroup {
    like('groupId', '%' + params.assignmentGroup + '%')   
   }
  }

    }
   render(view:'list', model:[ incidentInstanceList: results ])
   }

This is my view where I let the user select values:

   <g:form action="searchResults" controller="incident">
    <div class="dialog">
        <table>
            <tbody>
            <开发者_如何学Go;tr class="prop">
                <td valign="top" class="name">
                    <label for="status"><g:message code="incident.status.label" default="Status" /></label>
                </td>
                <td valign="top" class="value ${hasErrors(bean: incidentInstance, field: 'status', 'errors')}">
                    <g:select name="status" from="${incidentInstance.constraints.status.inList}"
                              value="${incidentInstance?.status}" valueMessagePrefix="incident.status" />
                </td>
            </tr>

            <tr class="prop">
                <td valign="top" class="name">
                    <label for="severity"><g:message code="incident.severity.label" default="Severity" /></label>
                </td>
                <td valign="top" class="value ${hasErrors(bean: incidentInstance, field: 'severity', 'errors')}">
                    <g:select name="severity" from="${incidentInstance.constraints.severity.inList}"
                              value="${incidentInstance?.severity}" valueMessagePrefix="incident.severity" />
                </td>
            </tr>

            <tr class="prop">
                <td valign="top" class="name">
                    <label for="assignmentGroup"><g:message code="incident.assignmentGroup.label"
                                                            default="Assignment Group" /></label>
                </td>
                <td valign="top"
                    class="value ${hasErrors(bean: incidentInstance, field: 'assignmentGroup', 'errors')}">
                    <g:select id="groupSelect" name="assignmentGroup.id" from="${app.Usergroup.list()}" optionKey="id"
                              value="" />
                </td>
            </tr>

            </tbody>
        </table>
    </div>

    <div class="buttons">
        <span class="button"><g:submitButton name="searchResults" class="searchResults" value="searchResults" /></span>
    </div>
</g:form>

For the first two values it is working fine. But with the assignmentGroup there is an error.

Error 500: Executing action [searchResults] of controller [app.IncidentController] caused exception:
IllegalArgumentException occurred calling getter of app.Usergroup.id

Incident class:

   package app
   class Incident {
      Usergroup assignmentGroup
      User assignId
      String severity
      String status


  static constraints = {

    servity(inList:["low","high"])
    status(inList:["Open","Closed","WIP"])
    createdOn()
    assignmentGroup()
}

}

Usergroup class:

 package app

 class Usergroup {

 String groupId

    static hasMany = [members : User]
    static belongsTo = User 

    static mapping = {
    table "group_" 
    }

    static constraints = {
     groupId(blank:false)
   }
   String toString (){ groupId }
   }


Did you try criteria something like that:

def results = criteria {
  and {
    like('status', '%' + params.status + '%')
    like('severity', '%' + params.severity + '%')
    assignmentGroup {
      like('groupId', '%' + params.assignmentGroup + '%')   
    }
  }
}


I think the problem is that you are using 'like' for the assignmentGroup. I would try something like...

def results = criteria {
                like('status', '%' + params.status + '%')
                like('servity', '%' + params.servity + '%')
                eq('assignmentGroup', params.assignmentGroup)

    }

Christian

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜