Is there an easy way to convert a coldfusion query record to a structure?
As I covered in my blog entry about converting a ColdFusion query to an array, you might not want to even go that far. A lot of times people want to convert queries and query records to other objects because they don't realize that they can reference rows and columns of a query without having to loop over the entire query:
Launch code in new window » Download code as text file »
However, if you want to convert the entire query to an array or queries, take at look at my previous post. But, if you want to convert just a single query row into a structure, I will show you a modification of my QueryToArray() method. This one, QueryToStruct() can convert a query to an array OR a single record to a struct:
Launch code in new window » Download code as text file »
This functions takes two arguments, the ColdFusion query and the row index of the record you want to convert to a structure. If you want to convert the entire query to an array of structures, just send in the query but do not send in a row index:
Launch code in new window » Download code as text file »
If you want to convert just a single record to a structure, then pass in the row index as the second argument:
Launch code in new window » Download code as text file »
Be aware that this function can return two types of data, an array (for an entire query) or a structure (for a single record). That is why the returntype attribute is set to "any". Again though, be sure you really want to add this kind of processing overhead. When possible, and when appropriate, just use the ColdFusion query object like it is an array of arrays.
Download Code Snippet ZIP File
Comments (6) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Ben, thanks for this post. I was just looking for something exactly like this for a project I am working on at the moment. This helped me save a couple of hours easily.
Cheers,
Kai
Posted by Kai Pradel on Jul 25, 2006 at 9:50 AM
Kai, always glad to help. If you ever need any other help, please feel free to contact me directly via the Ask Ben feature and I will try to get you a solution to your problem.
Thanks,
Ben
Posted by Ben Nadel on Jul 25, 2006 at 9:55 AM
Ben, thanks I was looking for this very same thing.
Posted by miguel on Nov 13, 2007 at 1:05 PM
Ben, thanks for this... you just saved me a solid couple hours of cutting and pasting, or alternatively trying to write something like this on my own!
Thanks again,Rick
Posted by Rick Anthony on Nov 15, 2007 at 2:36 PM
What about
<cfset myArray=ArrayNew(1)>
<cfloop query="someQuery">
<cfset myStruct = StructNew()>
<cfloop list="#columns#" index="i">
<cfset StructInsert(myStruct,i,Evaluate("someQuery.#i#"))>
</cfloop>
<cfset ArrayAppend(myArray,StructCopy(myStruct))>
</cfloop>
Posted by Miles on Mar 11, 2008 at 7:33 PM
<cffunction name="QueryToStruct" returntype="struct" output="false">
<cfargument name="query" type="query" required="true">
<cfset s = StructNew()>
<cfloop index="i" list="#ARGUMENTS.query.ColumnList#">
<cfset StructInsert (s, i, ARGUMENTS.query[i])>
</cfloop>
<cfreturn s>
</cffunction>
Posted by Ron Henry on Sep 10, 2008 at 2:44 AM