Converting a query to Array ... and Back again, for Flex sake...
There have been a few blog posts on how to convert a query into an array of structures. Here is Ben Nadel's, Here is Carlos Gallupa's, and there are more out there.
Now that you are a master at converting query to array-of-struct, how about converting it back to a query? No, not just for kicks, but perhaps for that crazy array you built, that you now want to pull via CFC into a Flex app ? (this is the reason I bothered, b/c u know that CF queries get auto-translated into ArrayCollections in Flex, don't you ??)
If you had an array of structures called "result," you can loop over the array, and reconstruct the query using the key names from your structs and dynamically populate the query set onr row at a time.
<cfset qrsResultSet = QueryNew(StructKeyList(result[1]))>
<cfloop from="1" to="#ArrayLen(result)#" index="j">
<cfset temp = QueryAddRow(qrsResultSet,1)>
<cfloop list="#StructKeyList(result[1])#" index="f">
<cfset temp = QuerySetCell(qrsResultSet,f,Evaluate("result[j].#f#"),j)>
</cfloop>
</cfloop>
Nice and simple. The only thing that could go wrong is if some of your structs have different keys, in which case you should do a bit of error checking, handling. Perhaps check if the struct key is in the query, and if not, add it.
|


There are no comments for this entry.
[Add Comment]