繁体   English   中英

grails使用父级和子级域的类似查询创建列表

[英]grails create a list with like query from parent and child domain

我正在使用grails 2.4.2。 我需要基于查询的类似关键字创建一个列表。 假设这是一个例子>>

    def results = c.list(max: iDisplayLength, offset: iDisplayStart) {
            and {
//                eq("activeStatus", ActiveStatus.ACTIVE)

            }
            if (sSearch) {
                or {
                    ilike('title', sSearch)
                    ilike('shortDesc', sSearch)
                }
            }
        }

在这里,我可以使用sSearch参数按字段进行搜索。 但是假设在此域中,我有一个名为Parent parent的父域实例。 现在,如果我也想使用sSearch检查parent.typeName的值,那我应该怎么做。 我尝试了如下>>

        or {
            ilike('title', sSearch)
            ilike('shortDesc', sSearch)
            ilike('parent.typeName', sSearch)
        }

但这会带来错误。 实际上,我想针对数据表执行此操作。 将父类字段保留在搜索选项下。 有什么办法用父类对象做到这一点? 你们能帮忙吗?

我的域音频域>>

    package streaming

class Audio {
    static mapping = {
        table('audio')
        version(false)
    }

    String title
//    StreamType streamType
    String shortDesc
    String filePath
    String imagePath
    String imageName
    int downloadCount
    boolean isActive

    static belongsTo = [streamType: StreamType]

    static constraints = {
        title(nullable: false, blank: false,unique: true)
        shortDesc(nullable: false, blank: false)
        filePath(nullable: false, maxSize: 2000)
        imagePath(nullable: false, maxSize: 2000)
        imageName(nullable: false)
        downloadCount(nullable: true)
    }
    String toString(){
        return title
    }
}

StreamType域>>

    package streaming

class StreamType {
    static mapping = {
        table('stream_type')
        version(false)
    }

    String typeName

    static hasMany = [audio: Audio, video: Video]

    static constraints = {
        typeName(nullable: false, blank: false)
    }
    String toString(){
        return typeName
    }
}

您可以访问StreamType通过将其放置在域属性streamType关闭或alias

通过关闭-

or {
    ilike('title', sSearch)
    ilike('shortDesc', sSearch)
    streamType {
        ilike('typeName', sSearch)
    }
}

通过别名-

or {
    ilike('title', sSearch)
    ilike('shortDesc', sSearch)
    createAlias('streamType', '_streamType')
    ilike('_streamType.typeName', sSearch)
}
 def results = c.list(max: iDisplayLength, offset: iDisplayStart) {
        and {
            eq("activeStatus", ActiveStatus.ACTIVE)
        }

        if (sSearch) {
            or {
                ilike('title', sSearch)
                ilike('shortDesc', sSearch)
                Parent{
                  ilike('typeName', sSearch)
                }
            }
        }
    }

不知道Parent是否进入或,但这就是您访问父属性的方式。

暂无
暂无

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

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