【问题标题】:Is there cfscript support for looping over a query with the group attribute是否有 cfscript 支持使用 group 属性循环查询
【发布时间】:2014-09-16 12:20:40
【问题描述】:

我在 Adob​​e ColdFusion 10 中运行以下代码。我想删除所有标签并在脚本中执行此操作。真实的代码比较复杂,这里只是一个演示的shell。

对此有任何 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 行为的方法。
  • Railo 实际上支持带有group 属性的cfscript 中的loop 命令,Adobe 承诺将所有功能从标签移植到带有CF9 和CF10 的脚本,但是,Adobe。所以走老路if(current_group_value eq old_group_value) continue;
  • 貌似是在CF11中完成的

标签: coldfusion coldfusion-10


【解决方案1】:

回答“不可能”。有解决方法,但我对此不感兴趣。 RAILO 做到了,Adobe CF11 做到了。但在 ACF10 中不可行

【讨论】:

  • 对于那些希望在 railo/cf11/无论最新的 os cfml 平台是什么的人来说,答案是在 cfscript 中使用 cf 标签的一般方法:cfloop (query="dogs" group="breed") { writeOutput(dogs.breed); cfloop () { writeOutput(dogs.name); }}
【解决方案2】:

有以下example of looping over a query in the Using CFScript statements documentation。如果您在查询中定义分组,那么它可能会满足您的需求。

for-in 构造(用于查询)

类似于循环遍历数组和结构,使用for-in构造,您可以在CFScript中循环查询对象。

示例

在本例中,查询结果集在变量arts 中可用,for-in 循环用于遍历结果集。 for-in 构造中使用的变量 row 是一个包含查询列作为键的结构。您可以使用 Arts.currentrow 来引用当前行。

<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>

注意:您必须在 queryname 前加上前缀才能访问记录计数或当前行等查询结果变量(如示例所示)。

【讨论】:

  • 不是我要找的。这会循环查询中的列。我想要似乎在 CF11 中可用的分组功能,但可能不是 CF10。 Adam Cameron 在blog.adamcameron.me/2014/09/documentation-for-cfscript.html 发布了很棒的 CFSCRIPT 资源
  • @GerryGurevich 是的,在我发布这个之后,我意识到你在询问更多关于 cfloop 的 group 属性的信息,然后循环查询本身。但是您发布的示例使用了标签,因此不确定您是否知道如何在 cfscript 中循环查询。我能够找到这个使用 Java HashMap 对数据进行分组的示例 - Grouping data in ColdFusion Using Hash
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2011-07-08
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多