Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails createCriteria how do i loop over restrictions

i can do this:

def criteria = Category.createCriteria();
def results = criteria.list{ 
                          like('categoryName', "%abc%") or
                          like('categoryName', "%qwe%") 

                        };

but how i do it for unknown number of string values? i would like do something like:

def results = criteria.list{  
for (str in strList){
like('categoryName', str) or
                          }
                        };

but ofcourse it doesnt work.

like image 239
zevel80300 Avatar asked Dec 22 '22 02:12

zevel80300


1 Answers

Use or { }:

def criteria = Category.createCriteria()
def results = criteria.list {
    or {
        strList.each { str ->
            like('categoryName', "%${str}%")
        }
    }
}
like image 130
Rob Hruska Avatar answered Jan 26 '23 06:01

Rob Hruska