grails创建一个包含来自父域和子域的查询的列表

时间:2015-06-07 11:42:20

标签: grails gorm grails-domain-class

我正在使用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 params按字段搜索。但是假设在这个域中我有一个名为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
    }
}

2 个答案:

答案 0 :(得分:1)

您可以StreamType关闭streamTypealias来访问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)
}

别名 -

index.php

答案 1 :(得分:0)

 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是否进入或者访问父属性。

相关问题