tracker issue : CF-3173191

select a category, or use search below
(searches all categories and all time range)
Title:

for/in with query has inconsistent behavior compared to cfloop

| View in Tracker

Status/Resolution/Reason: Closed/Withdrawn/Duplicate

Reporter/Name(from Bugbase): Sean Corfield / Sean Corfield (Sean Corfield)

Created: 04/23/2012

Components: Language, CFSCRIPT

Versions: 10.0

Failure Type:

Found In Build/Fixed In Build: Public Beta /

Priority/Frequency: Major / Some users will encounter

Locale/System: English / Mac All

Vote Count: 1

Duplicate ID:	CF-3808648

Problem Description: When you loop over a query with cfloop, qryname.currentRow is updated for each row and qryName.col is also updated to refer to each row's value of that column. When you use for ( row in qryname ) you can access row.col as expected, and qryname.currentRow still tracks the row number, but qryname.col stays on the first row. That's not consistent.

Steps to Reproduce: Set up a query with one column "name" and three rows: "bob", "dick" "jane" then run this code:

for (friend in friends){
 writeoutput('#friends.currentRow#:#friends.recordCount#>#friend.name#-#friends.name#;');
}

Actual Result: 1:3>bob-bob; 2:3>dick-bob; 3:3>jane-bob;

Expected Result: 1:3>bob-bob; 2:3>dick-dick; 3:3>jane-jane;

Any Workarounds:

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3173191

External Customer Info:
External Company:  
External Customer Name: SeanACorfield
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

cfloop for a query does not have any other variable reference to use for iteration and therefore the query variable is used. However 'for-in' construct by definition has a variable to be used for iteration and therefore it does not make sense to use both query variable and the iteration variable for the same purpose.
Comment by Rupesh K.
19689 | April 24, 2012 04:14:35 AM GMT
+1, should be fixed. Disagree with Rupesh's logic, and just because this is "by design" doesn't mean the design was actually good. It is a very common requirement to need to know the current row when looping over a recordset, and as this looping construct is supposed to be an analogue of a <cfloop query="">, it should behave as closely as possible to that. Rupesh's logic seems to be based around a theoretical knowledge of the programming construct, rather than a practical knowledge of how it will be used. CF is about making things easy, and this is a "fail" in this regard. -- Adam
Vote by External U.
19693 | May 28, 2012 01:12:36 AM GMT
Just an observation in conjunction with my vote here: Railo has the more sensible implementation in that the current row index is still maintained within the loop as queryName.currentRow, as one would expect. For the sake of cross-compatibility with Railo (not a significant consideration, granted, but you don't want Railo to be "better" than CF in this regard), and meeting what is clearly user expectations (Sean expects it, I expect it, the Railo community CFML users clearly expected it, given Railo implement it), this should be implemented. OpenBD does not seem to support this kind of loop at all yet, as far as I can glean from their docs.
Comment by External U.
19690 | July 10, 2012 02:03:34 AM GMT
Pah, ignore my previous comment. As Sean points out, queryname.currentRow DOES work in CF10 - phew - I'd misread Sean's original post and thought it said currentRow didn't work either. Still: the whole thing should just work the same as in <cfloop>. -- Adam
Comment by External U.
19691 | July 10, 2012 04:02:06 PM GMT
Oops, forgot this one existed and just raised it again, as it's just bitten me on the bum again: https://bugbase.adobe.com/index.cfm?event=bug&id=CF-3808648 I don't think "as designed" is a valid closure reason here, given the design is bad. Reopen & fix pls. -- Adam
Comment by External U.
19692 | August 20, 2014 02:41:25 AM GMT