Using The ColdFusion Query Object As A Complex Object Iterator

Posted August 23, 2006 at 2:31 PM by Ben Nadel

Tags: ColdFusion

Let me preface this by saying this is probably not useful, but kind of sweet anyway. Did you know that you can store complex data inside of a query object? It's true; you can pretty much store any kind of data inside of a ColdFusion query object. To demonstrate that, let's try to use the query object as an iterator of complex data structures. This, in and of itself, could be kind of cool, because the query object has some methods that are nice for iteration.

First, let's build a query from scratch with a single column that houses structures:

  • <!---
  • Create a query with only data column to hold
  • out complex objects.
  • --->
  • <cfset qGirls = QueryNew( "data" ) />
  •  
  • <!--- Add rows to query. --->
  • <cfset QueryAddRow( qGirls, 3 ) />
  •  
  • <!--- Set row data. --->
  • <cfset qGirls[ "data" ][ 1 ] = StructNew() />
  • <cfset qGirls[ "data" ][ 1 ].Name ="Thomas, Ashley" />
  • <cfset qGirls[ "data" ][ 1 ].IsSexy = true />
  • <cfset qGirls[ "data" ][ 1 ].HowSexy = "Very" />
  •  
  • <!--- Set row data. --->
  • <cfset qGirls[ "data" ][ 2 ] = StructNew() />
  • <cfset qGirls[ "data" ][ 2 ].Name ="Fisakis, Anne" />
  • <cfset qGirls[ "data" ][ 2 ].IsSexy = true />
  • <cfset qGirls[ "data" ][ 2 ].HowSexy = "Very" />
  •  
  • <!--- Set row data. --->
  • <cfset qGirls[ "data" ][ 3 ] = StructNew() />
  • <cfset qGirls[ "data" ][ 3 ].Name ="Cox, Christina" />
  • <cfset qGirls[ "data" ][ 3 ].IsSexy = true />
  • <cfset qGirls[ "data" ][ 3 ].HowSexy = "Very" />

As you see, for each query row, I am setting the value of [data] to a StructNew() and then treating the value as a struct. If you dump this out, you will see that everything is fine and dandy:


 
 
 

 
CFDump: Using ColdFusion Query As A Complex Object Iterator  
 
 
 

Now, let's loop over it and output the data values:

  • <!--- Loop over girls. --->
  • <cfloop query="qGirls">
  •  
  • <!--- Get current girl. --->
  • <cfset objGirl = qGirls.data />
  •  
  • <!--- Output data. --->
  • #objGirl.Name#<br />
  • #objGirl.IsSexy#<br />
  • #objGirl.HowSexy#<br />
  •  
  • <!--- Check for break. --->
  • <cfif NOT qGirls.IsLast()>
  • <br />
  • </cfif>
  •  
  • </cfloop>

Works as you would *hope* it to. Well, that's not 100% true. There are some caveats here. The biggest one is that you CANNOT reference the struct directly as in:

  • <cfset strName = qGirls.data.Name />

This throws the error:

Element DATA.NAME is undefined in QGIRLS

That is lame as that is what you would want to do naturally. To overcome this, you have two options: you can either do what I did above, by creating a intermediary "objGirl" structure, then referencing that, or you can use structure notation as in:

  • <cfset strName = qGirls[ "data" ][ 1 ].Name />

So that is a not-great caveat, but notice, on the other hand, that we can leverage other great query features like IsFirst() and IsLast(). No more comparing the current index to the array length (or however else you would do this sort of loop). Plus, you can use things like StartRow and EndRow:

  • <!--- Loop over first 2 rows. --->
  • <cfloop query="qGirls" startrow="1" endrow="2">
  • ....
  • </cfloop>

So, like I said, maybe not useful, but a kind of down-and-dirty way to use the query object. Plus, I am sure I am not alone on this, but isn't the query loop much nice to look at than an index loop?




Reader Comments

Aug 23, 2006 at 10:39 PM // reply »
153 Comments

Scary timing. I was just yesterday coding up a plugin-management CFC base-class/factory that uses objects inside of queries.

And yeah, cfloop with a query is so much sexier than without.


Aug 23, 2006 at 10:48 PM // reply »
74 Comments

Yeah, Rick, that's what i'm talking about! Sexy!


Aug 26, 2006 at 9:30 PM // reply »
1 Comments

Actually Ben your post was extremely helpful to me. I don't know if you use the report builder, but in that you are allowed to pass information from only one query into the report (not including any additional parameters passed in when the report is called). Anyway, before I read your post I had been imbedding subreports within subreports to get an output that was passable, but not great. Now I can pass all kinds of information along in the query by imbedding them in structures. Thanks!


Aug 27, 2006 at 11:39 AM // reply »
74 Comments

Cameron,

I have to say, I have only studies the report builder, but never actually used it. I am glad that this helps though. I am aware that you can pass queries into reports at run time, but passing a query of structs is wild :) Rock on.


Dec 15, 2008 at 3:42 PM // reply »
1 Comments

You player, you!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools