【问题标题】:Grails - countBy in _template counting all records instead of domain instanceGrails - _template中的countBy计算所有记录而不是域实例
【发布时间】:2013-02-07 01:18:19
【问题描述】:

我有 2 个简单的域模型。我正在尝试从投资组合实例的显示页面中统计已发布标志设置为 NO 的出版物。

我可以获得设置为 NO 的出版物总数,但无法获得 NO 标志的实例特定计数。无论我尝试过什么,我总是从所有记录中得到总回报。如何仅获取实例计数。

示例:用户加载 Porfolio 的显示页面(操作),显示出版物列表,通过计算该特定 Portfolio 的“已发布”标志 NO 来显示尚未发布的出版物的数字字段。

我尝试了 Publication.countByPublished('No')、${Publication.executeQuery("select count (published) from Publication where published=?",['No'])} 和其他各种方法,但就是不能似乎传递了该投资组合 ID,因此它仅计算已发布标志为 No 的该 ID 的出版物。

这个真的让我头疼

班级组合

hasMany = [publications:Publication]

Sring portfolioName

课堂发布

belongsTo = [portfolio: Portfolio]

String publicationName
String publicationContent
String published

static constraints = {
published (blank: false, inList: ["No","Yes"]) 

【问题讨论】:

    标签: grails grails-orm grails-2.0


    【解决方案1】:

    你试过了吗:

    def count = Publication.countByPublishedAndPortfolio("No", portfolioInstance)
    

    一种不太有效的方法是遍历出版物的投资组合集合并保留一个计数器:

    def count = 0
    portfolioInstance.publications.each {
      if (it.published == 'No') {
        count++
      }
    }
    

    【讨论】:

    • 在控制器中我在 Portfolio show.gsp 中有上述方法我有 ${count}
    • 抱歉编辑,def count = Publication... 在投资组合控制器中,${count} 在 show.gsp 中正确吗?
    猜你喜欢
    • 1970-01-01
    • 2019-07-31
    • 2017-10-17
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2014-08-25
    相关资源
    最近更新 更多