How to assign associative arrays to assign simple values

I created a code sample using the Microsoft SQL Server Northwind demo database. If you do not have access to this demo database, this is a simple (MS-SQL) script to create a table and data row for this question.

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

Here is the ColdFusion code:

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

In the above code, there is a boolean variable called "variables.useTempVar" at the top, which can be flipped to see the error I am getting.

It seems that direct assignment (when variables.useTempVar = false) from the query to the structure causes the structure values ​​to have the JVM type "coldfusion.sql.QueryColumn".

Another note: if this line of code:

<cfset variables['stProduct'][vcColName] = variables.temp>

changes to:

<cfset variables['stProduct'][vcColName] = variables['temp']>

The JVM data type will be "coldfusion.sql.QueryColumn".

temp- ( variables.useTempVar = true); JVM - , (java.lang.Integer, java.math.BigDecimal, java.lang.String ..).

, :

<cfset variables['stProduct'][vcColName] = qryNWProducts[vcColName].toString()>

. ? , .

: , . .

+4
2

@Leigh , . 1, : qryNWProducts[vcColName][1]

?

, ? , , .

, ( ColdFusion 11), serializeJSON/deSerializeJSON, . serializeJSON , "AJAX " JSON . JSON CF, :

NWProducts = deSerializeJSON( serializeJSON( qryNWProducts, 'struct' ) )[1]; .

Adobe serializeJSON, : true|false|struct|row|column, , .

, serializeQueryAs.

cfscript. queryExecute script . . cfscript trycf.com , script.

, , .

+3

@Abram , , .

CFML. . : query.columnName query.columnName[currentRow] ( currentRow - 1).

" ", query["columnName"] , .

CFML , , CFML , . .

+2

All Articles