Search code examples
grailsgrails-domain-classgrails-controller

Grails createCriteria() used inside Domain classes


Just got to know about the capability of createCriteria() method. Just wanna know that other than applying it on the Controller, is there a way to apply onto the domain classes as well? Probably on its own mapping to a property like:

static mapping = {
      additionalInfo: Page.createCriteria().list()
}

Solution

  • Perhaps you may want to simply create a new field based the target field like the below example:

    class myInfo {
        String additionalInfo
        String[] moreInfo  // a transient field
    
        getMoreInfo(){
            def myresultmap = createCriteria.list{
             // insert any other criteria shenanigans
            }
            return myresultmap
        }
        static transients = ['moreInfo']
    }
    

    In a controller return a view like normal with the Domain instance of class MyInfo Then use in view like:

    <g:each in="${domaininstancefromcontroller}">
    ${it.moreInfo[0]
    </g:each>
    

    see docs. Hope this helps.