【发布时间】:2018-09-26 13:51:14
【问题描述】:
我的系统中有几个函数应该从 CFML 转换为 CFSCRIPT。在处理这个项目时,我遇到了这样的情况:我的结构输出的结果(很多空行)比它应该的多,甚至查询也只返回一行。这是我在 CFSCRIPT 中的代码示例:
cfstoredproc( procedure="GetZip", datasource=Application.dsnRead ) {
cfprocparam( dbvarname="@Zip", value=trim(52401), cfsqltype="cf_sql_char", maxlength=5 );
cfprocresult( name="ZipResult" );
}
local.strZip = {};
strZip[ZipResult.RecID] = {
"City" : trim(ZipResult.City),
"State" : trim(ZipResult.State)
};
writeDump(strZip);
这是我得到的输出:
array
1 [undefined array element]
2 [undefined array element]
3 [undefined array element]
4 [undefined array element]
5 [undefined array element]
6 [undefined array element]
7 [undefined array element]
8 [undefined array element]
9 [undefined array element]
10 [undefined array element]
11 [undefined array element]
12 [undefined array element]
13 [undefined array element]
14 [undefined array element]
...
我想知道以结构输出查询结果并使用RecID 作为唯一键的最佳方式是什么?这个例子和上面的查询总是会同时返回 1 条记录,但我也想知道如果我需要用多条记录循环查询结果,这段代码将如何工作?
更新:我认为我找到了问题所在。 RecID 是我的数据库表中的自动增量 ID。例如,当我返回 RecID 时,该值可以是 56743。因此,如果我将 RecID 作为结构中的键传递,则会导致结构中出现如此多的行。我的问题是如何防止这种情况?有没有办法只设置密钥?
【问题讨论】:
-
之所以发生是因为 CF 认为 strZip 是一个数组,而不是一个结构。我无法使用发布的 sn-p 复制它。能否请您发布完整的功能?还有,什么版本的CF?
-
@Ageax 我不确定如何在这里为 ColdFusion 创建有效的 sn-p。我使用 CF 2016。我不确定这是否只是因为我试图将结果转储到屏幕上...
-
@espresso_coffee 把整个函数贴在上面。您还可以使用手动查询创建 sn-ps,即。 QueryNew(),不过还是先看看函数
-
@espresso_coffee - 我们肯定需要查看完整的代码,因为到目前为止它还不能重现。建议在您的应用中有所不同。
-
我真的不明白对一个有很多活动的问题的反对意见。但无论如何,我已经确认了这个问题以及我对范围界定问题的怀疑。 CFFiddle 显然配置了与您的服务器相同的范围搜索。我认为我比 TryCFs 更喜欢这个设置。无论如何:cffiddle.org/app/…
标签: struct coldfusion cfml cfquery cfloop