Left variable different from right variable inside cfloop over queries

I noticed a strange issue in Lucee.
For the following code:

<cfquery name="qGetData" datasource="#application.dsn#">
    select 1 col1, 2 col2, 3 col3
    from dual
</cfquery>
<cfset col1 = "bill">
<cfloop query="qGetData">
    <cfset col1 = variables.col1 & col1>
    <cfoutput>#variables.col1# #col1# #col2# #col3#</cfoutput>
</cfloop>

The result of the output is… bill1 1 2 3

This is problematic because it means that on the <cfset col1 = variables.col1 & col1> the col1 on the left means variables.col1, while the col1 on the right means qGetData.col1. This results in a very dangerous inconsistency.

In ACF, col1 is variables.col1 both on the left and on the right (of the = sign). This actually impacts me for a migration from ACF to Lucee as there are queries with * and columns in database like “label” and then, inside the loop, the label is also a variable. It’s virtually impossible to detect such situations as columns in database can be added (or removed).

Is there any setting that would make the behaviour similar with ACF? Is this a bug?

If you scope the columns to the query then you don’t have clashes: (add qGetData prefix to scope variable to query)… i’d actually expect this behaviour on CF also, but either was is best practice to scope your variables properly anyway to avoid dev confusion.

<cfquery name="qGetData" datasource="#application.dsn#">
    select 1 col1, 2 col2, 3 col3
    from dual
</cfquery>
<cfset col1 = "bill">
<cfloop query="qGetData">
    <cfset col1 = variables.col1 & col1>
    <cfoutput>#variables.col1# #qGetData.col1# #qGetData.col2# #qGetData.col3#</cfoutput>
</cfloop>
1 Like

@mihaimm, Yes. It works fine in both lucee & ACF. Both results are the same. But, you’ve any confusion on this means, please use the scope for the every variable. It’ll give a solution for some confusion.

1 Like

@cfmitrah & @dawesi Thank you very much for your answers.

I totally agree there are ways around this (by scoping the variables or by not using a variable that is also a column in the query we’re looping over).

My problem is that I’m migrating from ACF to Lucee and I have an application with roughly 50K CF files and more than 1K tables in the database. The application is tested and works fine on ACF, but some tests fail on Lucee because of the issue I mentioned. Of course, for an application this size that was created for the past 20 years, we don’t have 100% test coverage (far from it). For me it would be way more convenient to have left and right scopes be the same and consistent with the documentation. The closest scope is variables (Using scopes explicitly in code :: Lucee Documentation).