2015-06-07 35 views
0

我正在使用grails 2.4.2。我需要创建一个基于查询类似关键字的列表。假设这是一个例子>>grails用父类和子域的查询创建一个列表

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

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

我的域名 音频领域>>

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

流类型域>>

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 
    } 
} 
+0

您可以直接访问父属性,没有必要提及父母。尝试'ilike('typeName',sSearch)' – user1690588

+0

@ user1690588我试过你的方式,但它显示以下错误>>'消息:无法解析属性:typeName:streaming.Audio' –

+0

你能发布你的域名吗? – user1690588

回答

1

您可以通过将StreamType域属性置于streamType关闭或alias访问域属性。

通过closure-

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

他们两个都像魔术一样工作,非常感谢 –

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

不知道父进程内或但这是你将如何访问父母财产。

+0

感谢您的回复,但它显示此错误没有方法的签名:streaming.AudioService.Parent()适用于参数类型:(streaming.AudioService $ _tt__audioPaginateList_closure4_closure7_closure8)values:[ streaming[email protected]2c4cbe48]' –