简体   繁体   中英

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

I am using 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. But suppose in this domain I have a parent domain instance named Parent parent. Now if I also want to check the value of parent.typeName with sSearch, then how should I do this. 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 >>

    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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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