coldfusion - 如何获得关联数组符号以分配简单值

标签 coldfusion coldfusion-11

我使用 Microsoft SQL Server Northwind 演示数据库创建了示例代码。如果您无权访问这个演示数据库,这里有一个简单的 (MS-SQL) 脚本来为这个问题创建表和一行数据。

CREATE TABLE [dbo].[Products](
    [ProductID] [int] IDENTITY(1,1) NOT NULL,
    [ProductName] [nvarchar](40) NOT NULL,
    [SupplierID] [int] NULL,
    [CategoryID] [int] NULL,
    [QuantityPerUnit] [nvarchar](20) NULL,
    [UnitPrice] [money] NULL CONSTRAINT [DF_Products_UnitPrice]  DEFAULT (0),
    [UnitsInStock] [smallint] NULL CONSTRAINT [DF_Products_UnitsInStock]  DEFAULT (0),
    [UnitsOnOrder] [smallint] NULL CONSTRAINT [DF_Products_UnitsOnOrder]  DEFAULT (0),
    [ReorderLevel] [smallint] NULL CONSTRAINT [DF_Products_ReorderLevel]  DEFAULT (0),
    [Discontinued] [bit] NOT NULL CONSTRAINT [DF_Products_Discontinued]  DEFAULT (0),
 CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
    [ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Products] ON 
GO
INSERT [dbo].[Products] ([ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (1, N'Chai', 1, 1, N'10 boxes x 20 bags', 18.0000, 39, 0, 10, 0)
GO
SET IDENTITY_INSERT [dbo].[Products] OFF
GO

这是 ColdFusion 代码:
<cfset variables.useTempVar = false>

<cfquery datasource="Northwind2014" name="qryNWProducts">
SELECT TOP 1 * from Products;
</cfquery>

<cfdump var="#qryNWProducts#" label="qryNWProducts">

<cfset variables['stProduct'] = {}>
<cfloop index="vcColName" list="#qryNWProducts.columnlist#">
    <cfif variables.useTempVar>
        <cfset variables['temp'] = qryNWProducts[vcColName]>
        <cfset variables['stProduct'][vcColName] = variables.temp>
    <cfelse>
        <cfset variables['stProduct'][vcColName] = qryNWProducts[vcColName]>
    </cfif>
</cfloop>

<cfdump var="#variables['stProduct']#" label="variables['stProduct']">

<cfloop collection="#variables['stProduct']#" item="key"><cfoutput>
    variables['stProduct']['#key#'] JVM datatype = #getMetadata(variables['stProduct'][key]).getName()#<br>
</cfoutput></cfloop>

<br>
This always works:<br>
<cfset variables['aPhrase'] = "I ordered " &  variables.stProduct.ProductName & " for " & DollarFormat(variables.stProduct.UnitPrice) & ".">
<cfoutput>#variables['aPhrase']#<br></cfoutput>

<br>
With &quot;variables.useTempVar = false&quot;, the next line will throw a &quot;Complex object types cannot be converted to simple values. &quot; error.<br>
<cfset variables['aPhrase'] = "I ordered " &  variables['stProduct']['ProductName'] & " for " & DollarFormat(variables['stProduct']['UnitPrice']) & ".">
<cfoutput>#variables['aPhrase']#<br></cfoutput>

上面的代码在顶部有一个名为“variables.useTempVar”的 bool 变量,可以翻转它以查看我得到的错误。

看起来从查询到结构的直接赋值(当 variables.useTempVar = false 时)导致结构值是 JVM 类型“coldfusion.sql.QueryColumn”。

另一个注意事项:如果这行代码:
<cfset variables['stProduct'][vcColName] = variables.temp>

改为:
<cfset variables['stProduct'][vcColName] = variables['temp']>

JVM 数据类型将为“coldfusion.sql.QueryColumn”。

当使用点符号临时变量分配查询字段时(当 variables.useTempVar = true 时); JVM 数据类型是与数据库列类型(java.lang.Integer、java.math.BigDecimal、java.lang.String 等)非常匹配的简单类型。

我也经历过这样的陈述,但提供了一些奇怪的结果:
<cfset variables['stProduct'][vcColName] = qryNWProducts[vcColName].toString()>

这是问题。这是将简单值从查询传输到结构的最佳方式吗?被迫使用临时变量和点符号来完成这项工作似乎很奇怪。

评论:我一直认为点表示法和关联数组表示法是等价的。此代码示例似乎与该观点相矛盾。

最佳答案

@Leigh 是正确的,因为在将关联数组表示法与查询对象一起使用时,您需要提供行号。所以你可以像这样引用第 1 行:qryNWProducts[vcColName][1]
至于你的问题

Is this the best way to transfer the simple values from a query to a structure?



你确定你需要一个结构吗?您的问题并未真正指定用例,因此您完全有可能按原样使用查询对象会更好。

如果您确实需要将它作为结构体(并​​且因为您使用的是 ColdFusion 11),我建议您查看 serializeJSON/deSerializeJSON将其转换为结构。 serializeJSON有一个新属性,可以将查询对象正确序列化为“AJAX 友好”的 JSON 结构数组。然后,您可以将 JSON 反序列化为 CF 数组,如下所示:
NWProducts = deSerializeJSON( serializeJSON( qryNWProducts, 'struct' ) )[1]; 这将返回该查询对象中第一行的结构表示。

虽然从 the Adobe docs for serializeJSON 看不明显,第二个参数可以是以下之一:true|false|struct|row|column这将改变结果数据的格式。

这是一个 runnable example使用上述技术展示每个 serializeQueryAs选项。

开始将此类代码移入 cfscript 也是一个更好的做法。 . queryExecute非常易于使用,并使基于脚本的查询非常易于开发。见 How To Create a Query in cfscript trycf.com 上的教程,了解有关如何开发基于脚本的查询的更多信息。

最后一点,这有点偏离主题,但不使用 Hungarian Notation 是公认的最佳实践。命名变量时。

关于coldfusion - 如何获得关联数组符号以分配简单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30881579/

相关文章:

MySQL/ColdFusion 8 : sql-mode

java - 从 ColdFusion 调用 .jar

javascript - 是否值得为移动应用程序开发学习 cfclient API?

coldfusion - 需要创建一个带有分隔符的字符串数组的树状结构

javascript - 如何配置 ColdFusion Model Glue 3 使其不会在 AJAX 请求上重定向?

xml - 引用节点值时包含 ColdFusion 11 xml 标记

for-loop - Adobe ColdFusion 9 分页循环问题

sql - Coldfusion 循环查询并仅显示一次 html 标记

coldfusion - ColdFusion 11 中客户端和服务器端 cfc 之间有什么区别

forms - 为什么我不能使用结构语法将文件类型的表单字段传递给 CFFUNCTION?