user3068680
user3068680

Reputation: 61

how to grails find withCriteria or condition

String query='test'
def user = User.withCriteria {
    ilike('firstName', '%'+query+'%')
    or {ilike('lastName', '%'+query+'%')}
    or {ilike('email', '%'+query+'%')}
    }

above is the sample code i need to find those object if any one of three of these field( firstName lastName email ) contains query string thnks

Upvotes: 0

Views: 1918

Answers (2)

raffaeleambrosio
raffaeleambrosio

Reputation: 235

You can do in this way

 String query='test'
    def crit = User.createCriteria()
    def user = User.list{
      or{
        ilike('firstName', '%'+query+'%')
        ilike('lastName', '%'+query+'%')
        ilike('email', '%'+query+'%')
        }
      maxResults(1000)
      order("firstName")
     }

Upvotes: 0

Jacob
Jacob

Reputation: 4021

Try joining the conditions inside one or

or {
ilike('firstName', '%'+query+'%'),
ilike('lastName', '%'+query+'%'),
ilike('email', '%'+query+'%')
}

You can also turn on SQL debugging to see how your queries get built.

Upvotes: 1

Related Questions