Grails Criteria querying a list with ilike - grails

I want to query a list in criteria like this:
def patterns = ["abc%", "cde%"]
def criteria = MyEntity.createCriteria();
def results = criteria.list {
and {
patterns.collect {
not { ilike(name, it) }
}
}
is this basically possible? What do I have to do to get this query working?

Instead of collecting which will create a collection of the contents you need to simply iterate.
def patterns = ["abc%", "cde%"]
def criteria = MyEntity.createCriteria();
def results = criteria.list {
and {
patterns.each {
not { ilike('name', it) }
}
}

Related

Is there a limit to how many association levels you can reference in a Where query?

This works:
def query = Idea.where {manager.id == id}
This doesn't work:
def query = Idea.where {manager.profile.riding.id == id}
How can I get around this?
Construction Idea.where and Idea.withCriteria are not self-sufficient and not reliable. Always use construction Idea.createCriteria().
def riding = Riding.get(id)
def results = Idea.createCriteria().list() {
manager{
profile{
eq('riding', riding)
}
}
}

Grails CRUD list order by a field

I have a domain class Project as below
class Project {
String projectName
String projectCode
String techLead
String projectManager
Date deliveryDate
String currentPhase
Integer priority
}
I have controller as below
class ProjectController {
def scaffold = Project
def index = {
redirect(action:list,params:params)
}
def list = {
// displays only 10 records per page
if (!params.max) params.max = 10
[ projectList: Project.list( params ) ]
}
}
I would like to display the list of projects in the sorting order or priority. How can I implement that ?
change your list action to the below
def list = {
// displays only 10 records per page
if (!params.max) {
params.max = 10
}
params.sort = "priority"
params.order = "asc" // change to "desc" to sort in the opposite direction
[projectList: Project.list(params)]
}
A much shorter and more idiomatic way of doing this would be to use the dynamic methods on list that provide ordering:
def list = {
[projectList: Project.listOrderByPriority(max: params.max ?: 10)]
}

how can I read values from groovy g:select in my controller

I have several g:selects in my gsp that I want to use for filtering a query. How can I access their values in the controller? Here is a select that I have:
<td><g:select name="accts" from="${accountSelection}"/></td>
Here is how I populate the list
def filter() {
def allaccts = getAccountSelection()
[accountInstanceList:allaccts.accountInstanceList]
}
def getAccountSelection() {
params.max = 10
def accts = []
accts.add("all")
Account.list().each { item -> accts.add(item) }
[accountInstanceList: accts, accountInstanceTotal: Account.count()]
}
In your controller it's just another parameter. You can access it like this:
params?.accts?.each {
println it
}

How to use the withCriteria results in a new withCriteria query in Grails?

Consider the first query:
def books = Book.withCriteria {
eq("category", "fiction")
}
How can I use the result of this query in the next query to get all the authors who wrote these books?
I tried:
def authors = Author.withCriteria {
'in'("books", books)
}
but this is not working. What can I do to fix it?
Can't you just query the association directly?
def authors = Author.withCriteria {
books {
eq("category", "fiction")
}
}

how to trim a property in createCriteria

I am trying to trim a projection property but it does not let me do that.
def c = Book.createCriteria()
def books = c.list {
projections {
property ("title")
}
def now = new Date()
between('publishingDate', now-45, now+15)
}
I would like to trim the 'title' field in the criteria but it does not work. Any suggestions?
This will be possible in grails 2.2 using a sqlProjection:
projections {
sqlProjection 'trim(title) as title', ['title'], [java.sql.Types.VARCHAR]
}

Resources