简体   繁体   中英

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()
}

Just wanna know that other than applying it on the Controller, is there a way to apply onto the domain classes as well?

Criteria queries are not limited to controllers, you can apply them elsewhere using the same syntax that you would in a controller. However, the particular example you show there is probably going to be problematic because you are trying to use GORM inside of the mapping block which is used to configure GORM.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM