2012-12-27 21 views
0

Grails模型相当新颖,并且在我的数据库事务中使用服务时遇到了一些麻烦。使用g中的查询的Grails:选择服务

服务:

class ReportService { 

    def dataSource 
    def listDatatypeValues(Datatype dt) { 
     def sql = new Sql(dataSource) 
     def list = sql.rows (dt.statement) 
     return list 
    } 
} 

控制器:

def run(Long id) { 

    def reportInstance = Report.get(id) 
    def listPromptValues = populatePrompts(reportInstance) 

    if (!reportInstance) { 
     flash.message = message(code: 'default.not.found.message', args: [message(code: 'report.label', default: 'Report'), id])    
     return 
    } 
    [reportInstance: reportInstance, listPromptValues: listPromptValues] 
} 

def populatePrompts(Report rp){ 
    //for a prompt in the report, go out and get it's values 
    rp.prompts.each { 
     List list = reportService.listDatatypeValues(it.datatype) 
    } 
} 

查看片段:

<g:if test="${reportInstance?.prompts}"> 
    <li class="fieldcontain"> 
    <g:each var="prompt" in="${reportInstance.prompts}"> 
    <g:if test="${prompt.datatype.type == 'DropDown'}"> 
    <g:select id="prompt.name" from="${listPromptValues}" name="prompt.name" value="" noSelection="['':'']"/> 
     </g:if> 
    </g:each> 
    </li> 
</g:if> 

我们有一个报表对象,包含提示,又包含一个数据类型。对于任何给定的报告,当它在UI上拉起时,它会给出报告详细信息,然后列出提示值的提示值。问题是当前的设置将对象引用列为提示值,而不是从服务返回的值列表。

举例如下:报告1有2个提示:开始期限代码和结束期限代码。它们都使用术语代码作为数据类型,因为它是相同的SQL查询,并且从listDataTypeValues返回的列表将是存储在数据库中的70+术语代码的列表。

任何想法或方向?

我试着跟着this以下,但我无法得到它的工作。

谢谢!

+1

只是一个公共服务公告:使用groovy sql打败了数据库独立的目的。使用HQL或对象本身。 –

回答

1

您的populatePrompts函数没有返回有意义的值。如果用collectMany而不是each进行迭代,则表达式的值将是来自查询的所有结果的串联。尝试是这样的:

def populatePrompts(Report rp){ 
    rp.prompts.collectMany { 
     reportService.listDatatypeValues(it.datatype) 
    } //.unique() 
} 

您可能还需要调用独特的结果,以避免在您g:select输入重复。

+0

是的!非常感谢你,那样做。现在还有一个问题。它返回一张地图列表。任何方式拉只是值,而不是关键? – idonaldson

+0

在列表成员(例如'listPromptValues = populatePrompts(reportInstance)*。values()')上调用'values()',您将得到一个列表列表。如果你选择了一个单独的列,将其平坦化,你就可以得到结果列表。 – ataylor