简体   繁体   English

在Domain类中使用的Grails createCriteria()

[英]Grails createCriteria() used inside Domain classes

Just got to know about the capability of createCriteria() method. 刚刚了解createCriteria()方法的功能。 Just wanna know that other than applying it on the Controller, is there a way to apply onto the domain classes as well? 只是想知道,除了将其应用于Controller之外,还有没有一种方法可以应用于域类? 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? 只是想知道,除了将其应用于Controller之外,还有没有一种方法可以应用于域类?

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. 但是,您显示的特定示例可能会出现问题,因为您试图在用于配置GORM的mapping块内部使用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: 在控制器中,使用类MyInfo的Domain实例返回一个正常的视图,然后在如下视图中使用:

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

see docs . 参见文档 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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