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

但它给出了错误。其实我想为数据表做这个。将父 class 字段保留在搜索选项下。有什么办法可以用父 class 对象做到这一点吗?你们能帮忙吗?

我的领域 音频域 >>

    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
    }
}
 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 是否在 or 内部,但这是您访问父对象的方式 属性。

您可以访问 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)
}