0
votes

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
    }
}
2
You can directly access parent properties, no need to mention parent. Try ilike('typeName', sSearch) - MKB
@user1690588 I have tried your way but it shows the following error >> Message: could not resolve property: typeName of: streaming.Audio - Sumon Bappi
Can you post your domains? - MKB
I have post my domain, thanks in advance - Sumon Bappi

2 Answers

1
votes

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)
}
0
votes
 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.