Grails simple-blog BlogEntry类如何在一个简短的内容中使用FindByTagAndLockedAndPublish

时间:2014-10-06 05:43:14

标签: grails grails-2.0 grails-plugin grails-domain-class grails-controller

我在grails中使用简单博客插件。我有代码

class OodlesBlogEntry extends org.grails.blog.BlogEntry implements Taggable, Commentable{

Boolean isProtected = false;

}


  def entries =  OodlesBlogEntry.findAllByTag(params.tag.trim(), [max:5, offset:params.offset, sort:"dateCreated", order:"desc"])

entries.findAll { it.published }

这些工作正常,但问题是我想同时使用这两个语句,以便它可以给我完美的输出。类似的东西。

def entries = OodlesBlogEntry.findAllByTagAndPublishAndLocked(params.tag.trim(), true, false [max:5, offset:params.offset, sort:"dateCreated", order:"desc"])

但这句话不起作用。请帮我解决这个问题。

将两者放在一行时出错。

| Error 2014-10-06 12:40:52,133 [http-bio-8080-exec-5] ERROR errors.GrailsExceptionResolver  - InvalidPropertyException occurred when processing request: [GET] /blog/tagged/android
No property found for name [tag] for class [class com.oodles.blog.OodlesBlogEntry]. Stacktrace follows:
Message: No property found for name [tag] for class [class com.oodles.blog.OodlesBlogEntry]
    Line | Method
->>  104 | methodMissing in org.grails.datastore.gorm.GormStaticApi
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    569 | byTag         in com.oodles.blog.BlogController$$EOrtPHk8
|    198 | doFilter . .  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter      in grails.plugin.cache.web.filter.AbstractFilter
|     53 | doFilter . .  in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     49 | doFilter      in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
|     82 | doFilter . .  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|    270 | doFilter      in com.planetj.servlet.filter.compression.CompressingFilter
|   1145 | runWorker . . in java.util.concurrent.ThreadPoolExecutor
|    615 | run           in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . . in java.lang.Thread

1 个答案:

答案 0 :(得分:0)

错误的原因是findAllByTag不是GORM动态查找器。它是可标记插件添加到实现Taggable接口的任何域类的方法。

由于findAllByTag不是动态查找器,因此您无法将其与其他动态查找器结合使用,例如findAllByTagAndPublish

安装可标记插件时,会向您的域TagTagLink添加另外两个类。后者将标签与已用其标记的域对象连接,例如,

mysql> select * from tag_links limit 5;
+----+---------+--------+---------+-----------+
| id | version | tag_id | tag_ref | type      |
+----+---------+--------+---------+-----------+
|  1 |       0 |      1 |       1 | blogEntry |
|  2 |       0 |      2 |       1 | blogEntry |
|  3 |       0 |      3 |       1 | blogEntry |
|  4 |       0 |      4 |       2 | blogEntry |
|  5 |       0 |      5 |       2 | blogEntry |
+----+---------+--------+---------+-----------+

您当然可以编写一个SQL查询来检索具有特定标记的所有已发布,未锁定的博客条目,但它不会很漂亮。你甚至可以使用HQL,但我个人不打扰。我知道你当前的方法要求你运行2个查询,其中只需要1个,但这真的很重要吗?除非你有数百/数千篇博文(这似乎不太可能),否则它不会产生太大的实际差异。

更新

或者,您可以通过以下方式获取具有特定标记的所有博客条目:

def entries =  OodlesBlogEntry.findAllByTag(params.tag)

然后你可以在Groovy代码中过滤掉未发布或锁定的标签,排序和分页:

def filteredEntries = entries.findAll{ it.published && !it.locked }
def sortedEntries = filteredEntries.sort { it.title }

def max = 5 
def offset = params.offset

// TODO check the size of sortedEntries and adjust the pagination params to prevent an 
// IndexOutOfBoundsException
def entriesPage = filteredEntries.sort[offset..<offset + max]