CFOUTPUT cannot take a function as its query
Every now and again, I come across something in ColdFusion that really bugs me. This is today’s annoyance.
When using <cfoutput> to loop over a query, the query attribute cannot be a function name. For example, I have a CFC with a method getRoles(), which returns a query.
I would have expected that the following would work:
<cfoutput query="getRoles()">
...
</cfoutput>
…but this throws an error. So I am forced to add extra code to work around it:
<cfset qRoles = getRoles() />
<cfoutput query="qRoles">
...
</cfoutput>
I’m sure there’s a logical reason why this should be the case, and that someone will tell me what it is, but I just find it irritating.
Posted at 4:27 PM on 25 June, 2009, in ColdFusion
What about
<cfoutput query="#getRoles()#">
@Raul - it won't work because you can't give it the actual query, you must give it the name of the query. That will throw the "Complex object types cannot be converted to simple values." error. same as if you'd tried <cfoutput query="#qryName#">
CF needs to have a reference to query outside of its use in the cfoutput tag which is really just a pointer to the query. Also, You SHOULD be scoping your variables within the output, which means you need to reference the query by name. Even if you don't scope the variables, the scope is implied and the reference needs to exist.
<cfset qRoles = getRoles() />
<cfoutput query="qRoles">
#qRoles.Name#
</cfoutput>
I see, didnt even tried it... never ran into that scenario.. I always define my variables in the controller layer anyway
Yes, some tags take variable name only. CFOUTPUT query is one of them.
frustrated when you don't know what a tag takes? quickly look it up at: http://www.cfquickdocs.com/cf8/