gpt4 book ai didi

coldfusion - 在 Coldfusion 9 中使用 Coldfusion.sql.QueryTable.next()

转载 作者:行者123 更新时间:2023-12-04 06:20:45 25 4
gpt4 key购买 nike

我正在尝试编写一个自定义标记,它将以特殊方式遍历 cfquery 对象。我找到了这个页面:http://www.zrinity.com/developers/mx/undocumentation/query.cfm概述了如何使用底层 java 方法来导航结果集,但它似乎在 CF9 中不起作用。

我可以打电话.next() , .previous() , .first() , 和 .last()就好了,每个方法都会更新query.currentRow ,但引用 query.columnName始终返回第一行的值,而不是 currentRow。

例子:

<cfquery name="testQuery" datasource="source">
SELECT FooName FROM NumberedFoos
</cfquery>

<cfloop from="1" to="3" index="i">
<cfoutput>#testQuery.currentRow# =&gt; #testQuery.fooName#</cfoutput><br />
<cfset testQuery.next()>
</cfloop>

产生:
1 => Foo 1
2 => Foo 1
3 => Foo 1

我知道我可以使用类似 testQuery.fooName[testQuery.currentRow] 的东西,但这对于我为其制作自定义标签的人来说是非常不受欢迎的。上述链接中描述的功能是否已从 CF9 中删除?如果是,是否有替代方案?

编辑

为了扩展原因,客户需要一个自定义标签,允许他们“断言”有关查询的某些内容。客户对 CF 的理解水平很低,但编写 SQL 非常扎实。他们想要的最终结果类似于:
<cfquery name="purchaseTotals">
SELECT PurchaseId, Total FROM Purchases
</cfquery>

<CF_ASSERT query="purchaseTotals">
purchaseTotals.Total gte 0
</CF_ASSERT>

所需的输出将是一个 html 表,每一行都是查询失败断言的行。所以对我来说,CF_ASSERT 标签需要能够更新当前行。

编辑2:

主要的挑战是在标签主体中允许 html,同时仍然从适当的行替换查询值:
<CF_ASSERT query="purchaseTotals">
<CF_CONDITION expression="purchaseTotals.Total gte 0">
<!---error message when expression is false--->
<cfoutput>
Purchase #purchaseTotals.purchaseId# has a negative total!
</cfoutput>
</CF_CONDITION>
<CF_CONDITION expression="purchaseTotals.Total eq ''">
#PurchaseTotals.purchaseId# has a null total, this may be caused by:
<ul>
<li>Edge Case 1</li>
<li>Edge Case 2</li>
</ul>
</CF_CONDITION>
<CF_ASSERT>

这里的输出类似于:

购买120有负总数!
购买 157 有负总数!
购买 157 的总数为空,这可能是由以下原因造成的:
  • 边缘案例 1
  • 边缘案例 2
  • 最佳答案

    Was the functionality described in the above link removed from CF9?



    自这篇文章于 2006 年撰写以来,内部内容确实发生了变化。但我怀疑您所描述的确切功能可能不存在于任何 mx 版本中。您的代码与链接示例之间的主要区别在于 <cfoutput query=".."> 的用法(不仅仅是普通的 <cfoutput> )。 query属性显然在评估变量时提供了一些额外的上下文。删除它(就像在你的例子中一样),结果是“第一行的值,而不是 currentRow。”。即使在 MX6 下,这对后续版本也不是好兆头。那个确切的功能可能没有被删除。它只是从一开始就没有用。

    If so is there an alternative?



    就像我之前说的,最干净的方法是使用数组概念,即 #query.column[row]# .鉴于您似乎拒绝了该选项,您基本上只剩下 evaluate() .您需要在父标签内循环查询。然后使用 evaluate处理子标签表达式和内容。它不是特别优雅或简单的 IMO。但我认为这可能是好的,因为它没有数组符号,或者某种仪式牺牲。

    断言.cfm
    <cfparam name="attributes.query" type="string">

    <cfif thisTag.ExecutionMode is 'start'>
    <!--- validate attributes.query is a query object --->
    <cfif not ( structKeyExists(caller, attributes.query) AND IsQuery(caller[attributes.query]) )>
    <cfthrow message="Attributes.query [#attributes.query#] is undefined or not a query object">
    </cfif>
    </cfif>
    <cfif thisTag.ExecutionMode is 'end'>
    <cfset variables[attributes.query] = caller[attributes.query]>
    <cfloop query="variables.#attributes.query#">
    <cfloop array="#thisTag.assocAttribs#" index="subTag">
    <cfset variables.matchFound = evaluate(subTag.expression)>
    <cfif variables.matchFound>
    <cfoutput>[#currentRow#] #evaluate(DE(subTag.Content))#</cfoutput><hr>
    </cfif>
    </cfloop>
    </cfloop>
    </cfif>

    条件.cfm
    注意:做 不是 使用 <cfoutput>标签内容中的标签。
    <cfparam name="attributes.expression" type="string">
    <cfif thisTag.ExecutionMode is "start">
    <cfassociate baseTag="CF_ASSERT">
    </cfif>
    <cfif thisTag.ExecutionMode is "end">
    <cfset attributes.content = thisTag.GeneratedContent>
    <cfset thisTag.GeneratedContent = "">
    </cfif>

    client has a pretty low level understanding of CF, but are pretty solid writing SQL



    说了这么多,事情以这种方式实现是因为它是最好的方法还是因为它与编写 SQL 最相似,即舒适?

    关于coldfusion - 在 Coldfusion 9 中使用 Coldfusion.sql.QueryTable.next(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6600363/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com