有没有适用于使用group属性循环查询的cfscript支持?

3
我正在Adobe ColdFusion 10中运行以下代码。我想在脚本中删除所有标签。真实的代码更加复杂,这只是一个演示用壳。
是否有任何cfscript支持?您应该能够直接复制和粘贴此代码作为我尝试实现的示例。
<h1>Task Migration</h1>
<cfscript>
    id=0;
    commentid=0;
    qryTasks = queryNew("tasknumber,name,commentid,comment"
                        ,"integer,varChar,integer,varChar"
                        ,[ 
                            { 
                                tasknumber : ++id
                                ,name : "Task Name for #id#"
                                ,commentid: ++commentid
                                ,comment : "comment #commentid# on tasknumber #id#"
                            }
                            ,{ 
                                tasknumber : id
                                ,name : "Task Name for #id#"
                                ,commentid: ++commentid
                                ,comment : "comment #commentid# on tasknumber #id#"
                            }
                            ,{ 
                                tasknumber : ++id
                                ,name : "Task Name for #id#"
                            }
                            ,{ 
                                tasknumber : ++id
                                ,name : "Task Name for #id#"
                                ,commentid: ++commentid
                                ,comment : "comment #commentid# on tasknumber #id#"
                            }
                        ] 
                );  
    writedump(var:qryTasks, label:"starting query");
    traceLog=[];
</cfscript>

<cfloop query="qryTasks" group="tasknumber">
        <cfscript>
            arrayAppend(traceLog, "Make a ticket for #qryTasks.name#");
        </cfscript>
        <cfloop group="commentID">
            <cfscript>
                if (trim(qrytasks.comment) != ''){
                    arrayAppend(traceLog, "Add comment to #qryTasks.name#: #qrytasks.comment#");
                };
            </cfscript>
        </cfloop>
</cfloop>

<cfdump var="#tracelog#" label="Stuff that happened in the loop" />

据我所记,group不是在cfscript中的for循环中可用的属性。如果您需要经常使用此功能,请创建一个模拟group行为的方法。 - wiesion
1
Railo实际上支持在cfscript中使用loop命令,并带有group属性。Adobe承诺在CF9和CF10中将所有标签功能移植到脚本,但是呢,Adobe就是这样。因此,请采用老派的方式:if(current_group_value eq old_group_value) continue; - wiesion
似乎是在CF11中完成的。 - Gerry Gurevich
2个回答

4

回答是“不可能”。虽然有变通方法,但我对此不感兴趣。RAILO可以做到,Adobe CF11也可以做到,但在ACF10中无法实现。


2
对于那些想在railo/cf11/无论是最新的os cfml平台上实现它的人来说,答案是使用cfscript中的通用方式:cfloop (query="dogs" group="breed") { writeOutput(dogs.breed); cfloop () { writeOutput(dogs.name); }} - Kevin B

-1

在使用CFScript语句文档中,有一个循环查询示例。如果您在查询中定义了分组,则可能会得到您需要的结果。

for-in construct (for query)

Similar to looping over array and struct, using for-in construct, you can loop over query object in CFScript.

Example

In this example, query resultset is available in the variable arts and the for-in loop is used to loop over the resultset. The variable row used in the for-in construct is a struct that contains query columns as keys. You can use arts.currentrow to reference the current row.

<cfquery name="arts" datasource="cfartgallery"> 
    select * from art 
</cfquery> 
<cfscript> 
    cols = listToArray(listsort(arts.columnlist, "textnocase")); 
    for(row in arts) 
    { 
        for(col in cols) 
            writeoutput(arts.currentrow & " ..." & col & ": " & row[col] & "<br>"); 
        writeoutput("<hr>"); 
    } 
</cfscript>

Note: You have to prefix queryname to access the query result variables such as recordcount or currentrow (as shown in the example).


不是我要找的。这个循环遍历查询中的列。我想要的是在CF11中似乎可用但可能不适用于CF10的分组功能。Adam Cameron在http://blog.adamcameron.me/2014/09/documentation-for-cfscript.html上发布了很棒的CFSCRIPT资源。 - Gerry Gurevich
@GerryGurevich 在我发布这篇文章后,我意识到你更关心 cfloop 的 group 属性而不是循环查询本身。但是,你发布的示例使用标签,所以我不确定你是否知道如何在 cfscript 中循环查询。我找到了一个使用 Java HashMap 分组数据的示例 - 使用哈希在 ColdFusion 中分组数据 - Miguel-F

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接