简体   繁体   English

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

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

I am using grails 2.4.2. 我正在使用grails 2.4.2。 I need to create a list based on like keyword of query. 我需要基于查询的类似关键字创建一个列表。 Suppose this is an example >> 假设这是一个例子>>

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

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

Here, I can search by field with sSearch params. 在这里,我可以使用sSearch参数按字段进行搜索。 But suppose in this domain I have a parent domain instance named Parent parent. 但是假设在此域中,我有一个名为Parent parent的父域实例。 Now if I also want to check the value of parent.typeName with sSearch, then how should I do this. 现在,如果我也想使用sSearch检查parent.typeName的值,那我应该怎么做。 I have tried as follows >> 我尝试了如下>>

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

But it gives error. 但这会带来错误。 Actually I want to do this for datatable. 实际上,我想针对数据表执行此操作。 To keep the parent class field under search option. 将父类字段保留在搜索选项下。 Is there any way to do this with parent class object? 有什么办法用父类对象做到这一点? Can you guys please help ? 你们能帮忙吗?

My Domain Audio Domain >> 我的域音频域>>

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

You can access StreamType domain properties by placing them in streamType closure or by alias . 您可以访问StreamType通过将其放置在域属性streamType关闭或alias

By closure- 通过关闭-

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

By alias - 通过别名-

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

Not sure if Parent goes inside the or but that is how you would access a parent property. 不知道Parent是否进入或,但这就是您访问父属性的方式。

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

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