hamza
hamza

Reputation: 1

Criteria in grails

Could you tell me what's wrong in my Criteria here?

    def users
    def u = User.createCriteria()
    users = u.list (max: max, offset: offset) {

        eq("account",account)

        and {

            if(teacherName != null && teacherName != ""){
                like("userName", "%"+teacherName+"%")

            }

            if(mobileNumber != null && mobileNumber != ""){
                like("mobileNumber", "%"+mobileNumber+"%")

            }

            eq("status", Status.ACTIVE)
            eq("userType","Account Teacher") 
        }


    }
    return users
}

the list returned empty why?

Upvotes: 0

Views: 70

Answers (1)

Dónal
Dónal

Reputation: 187399

The code you posted is unnecessarily verbose. An equivalent implementation is:

def users = User.withCriteria(max: max, offset: offset) {

    eq("account", account)

    if (teacherName) {
        like("userName", "%${teacherName}%")
    }

    if (mobileNumber) {
        like("mobileNumber", "%${mobileNumber}%")
    }

    eq("status", Status.ACTIVE)
    eq("userType", "Account Teacher") 
}

I can't say why this isn't working because I don't know what the User domain class looks like or what behaviour the query is supposed to exhibit, but it ought to be easier to debug a more concise implementation.

Upvotes: 1

Related Questions