
Wish List
Tags
adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript max2007 max2008 misc open source programming railo software technology ui
Recent Entries
The Pirate Bay Sold!
Railo Documentation
iPhone Navi Software Now Available!
iPhone OS3 Cracked!
Search
RSS
Subscribe
Blogroll
An Architect's View
Ben Forta
CFSilence
Coldfusion Jedi
Rey Bango
TalkingTree
Var Scoping Queries
Everyone says that we should var scope all of our variables and I make a concerted effort to do just that. But for some reason I've never considered the idea of
var scoping my queries. For some strange reason I automatically assumed that the name we give to our queries is automatically in the var scope.
It came as quite a surprise to me to find out that it isn't. Now some of you may be saying 'Well Duh', but hey I'm not perfect and I like to share when I find something out that I didn't know or never even considered (which is the case here).
So if you already knew that you had to initialize the variables you used for queries in the var scope prior to using them in your cfquery you can stop reading now. If you didn't and want to see how interesting things can happen, keep reading.
Consider the following component:
<cfcomponent>
<cffunction name="functionA" returntype="void" access="public">
<!--- <cfset var query1=""> --->
<cfquery name="query1" datasource="cfartgallery">
select * from art
</cfquery>
</cffunction>
<cffunction name="functionB" returntype="query" access="public">
<!--- <cfset var query1=""> --->
<cfquery name="query1" datasource="cfartgallery">
select * from artists
</cfquery>
<cfset functionA()/>
<cfreturn query1/>
</cffunction>
</cfcomponent>
It's pretty simple, functionA does a query and returns nothing back, functionB does a different query, calls functionA and then returns query1, both queries have the same name.
I then have a cfm template that simply invokes functionB as follows:
<cfscript>
myObj = createobject("component","testComponent");
rsResults = myObj.functionb();
</cfscript>
<cfdump var="#rsResults#"/>
Naturally it's not good practice to name you queries the same but sometimes its going to happen. Anyway one would "expect" the results to be the list of artists from the artists table, but what we actually get back is the results from functionA.
Uncommenting the two commented out cfset's and you get the right information back as I initially expected.
You might be thinking that you wouldn't do something so silly but what if you extend a component that just so happens to make use query with the same name in your component...err whoops....
Naturally this problem doesn't show itself when instantiating a completely separate component so you are ok there, but when extending a component or having two methods in the same component with the same query name you could have some problems.
In the end it's better to var scope the variable names you use for your queries too!
Happy Coding...


http://www.schierberl.com/cfblog/index.cfm/2007/12...
That isn't everything, just the ones he added in that release. Which just goes to show that we should all be using a tool like var scoper since it's easy to overlook something.
Every little bit help in regards to getting an understanding about var scoping. It amazes me sometimes how developers can put var scoping on the back burner instead of taking the 10-15 seconds to var scope a given variable. It's kind of like the "until it really effects me and my app I am just going to leave it alone". Ya know?
Your example is a great one. I was at a Saccfug meeting (Sacramento) just recently and many people did not know that you need to var scope variable that pertain to certain functions in cf (loops, etc).
http://www.gold4power.com