【问题标题】:Could not resolve property: name of: bookmarks.TagReference - Grails, Search无法解析属性:名称:bookmarks.TagReference - Grails,搜索
【发布时间】:2014-03-28 22:35:14
【问题描述】:

晚上好, 我正在从 Graeme Rocher 的《Grails 权威指南》一书中创建一个书签应用程序。在这个应用程序中,我有 4 个域:用户、书签、标签和 TagReference。 书签

hasMany = [tags:TagReference]
def belongsTo = User
User user   

URL url
String title
String notes

标签参考

Bookmark bookmark
User user
Tag tag

标签

String name

我正在尝试不仅按标题和注释搜索书签,还按标签搜索。

书签控制器:

def search = {
    def criteria = Bookmark.createCriteria()
    def b = criteria.list {
                or {
                    ilike('title',"%${params.q}%".toString())
                    ilike('notes',"%${params.q}%".toString())

                    tags {
                        ilike('name',"%${params.q}%".toString())}
                    }
            }

           def fromDelicious = null
           try {
               fromDelicious = deliciousService.findRecent(session.user)
           }
           catch(Exception e) {
               log.error('Failed to invoke del.icio.us: ' + e.message, e)
           }
        render(view:'list',model:[bookmarkInstanceList:b.unique(),deliciousResults:fromDelicious])

/layouts/main

<g:form controller="bookmark" action="search" >
<input type="text" name="q" /> <input type="submit" value="search" />

如果我删除 tags 部分,它可以正常工作(当然仅用于按标题和注释进行搜索)。但是有了它,我得到了这个错误:

Executing action [search] of controller [bookmarks.BookmarkController] caused exception: could not resolve property: name of: bookmarks.TagReference

我知道这是因为我的 TagReference 没有属性名称,但是我怎样才能得到它呢?

【问题讨论】:

    标签: search grails tags bookmarks


    【解决方案1】:

    根据您的域模型,您需要通过标签集合访问 Tag.name 属性。所以你的标准看起来像这样:

    def criteria = Bookmark.createCriteria()
    def b = criteria.list {
                or {
                    ilike('title',"%${params.q}%".toString())
                    ilike('notes',"%${params.q}%".toString())
    
                    tags {
                        tag {
                            ilike('name',"%${params.q}%".toString())}
                        }
                    }
            }
    

    【讨论】:

      猜你喜欢
      • 2017-09-30
      • 2013-05-16
      • 2013-08-08
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 2018-10-17
      相关资源
      最近更新 更多