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 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools