Nested queries in ColdFusion

The applications i’m currently working on are to be deployed on ColdFusion 7. I ran into an old friend (read bug) related to nested queries.

The issue is in CF7 if you have a loop nested inside of another loop. If you are inside the inner loop and you try to display a column from the outer loop, it always gets the item from the first row (so outerLoop[1].name for instance).

We will be moving to CF8 in the near future (yey!), so I was curious to see if the behaviour had changed. I built the following page to test it out.


<!--- grab a directory and return a query --->
<cfset myDir = "c:\">

<cfdirectory action="list" name="outer" directory="#myDir#">

<h3>nested query test</h3>

<ul>
<!--- loop over the directories retrieved (outer loop) --->
<cfoutput query="outer">

<!--- If the current row is another directory --->
<cfif outer.type eq "Dir">
<li>

<!--- output the directory in the outer loop to see what should be displayed in the inner loop --->
<ul>#myDir##outer.name#

<!--- get the contents of the directory --->
<cfdirectory action="list" name="inner" directory="#myDir##outer.name#">

<!--- loop over the sub directories (inner loop) --->
<cfloop query="inner">
<li>#myDir##outer.name##inner.name#</li>
</cfloop>

</ul>
</li>
</cfif>
</cfoutput>
</ul>

In CF7 when I run this I get the first directory displayed for every output of #outer.name# in the statement #myDir##outer.name##inner.name#, but in CF8 every occurence of #outer.name# is different, which is what I have been expecting it to do for the last few versions of ColdFusion ;)

EDIT: I figured someone must have discussed this as well and found a blog post from Ben Nadel who has also linked to Rick Osborne.


About this entry