I have a function in my "ProjectListener" called "getMyProjectsAsXML"
where I get a list of projects from my ProjectGateway to build an XML
string. After returning the query, I loop through my ProjectInfo
query to build my XML. In the process of building the XML, I need to
perform some additional queries to set some attributes in the XML.
For some reason, these additional queries are returning the same
values each time, even though I verified that the project_sid that I
am passing in does change. I started dumping some variables and found
that my "var" scoped variables within my function is not changing. In
the code, I var scope ProductID, then assign it equal to the
project_sid, which is from my query. But when I dump the values, I
get the output shown below.
<cfcomponent name="ProjectListener" displayname="ProjectListener"
output="no" extends="MachII.framework.Listener"
hint="ProjectListener for Hello Mach-II Sample Application">
<cffunction name="configure" access="public" output="false"
returntype="void"
hint="Configures this listener as part of the Mach-II framework">
<cfset VARIABLES.DSN = getProperty("DSN")>
<cfset VARIABLES.projectGateway = CreateObject("component",
"ProjectGateway").init(#VARIABLES.DSN#) />
<cfset VARIABLES.projectDAO = CreateObject("component",
"ProjectDAO").init(#VARIABLES.DSN#) />
</cffunction>
<cffunction name="getMyProjectsAsXML" access="public" output="false"
returntype="string"
hint="returns a list of projects as XML">
<cfargument name="event" type="MachII.framework.event"
required="yes" />
<cfset var ProjectsInfo =
VARIABLES.projectGateway.getMyProjects(event.GetArg("userSession")) />
<cfset var ProjectID = 0 />
...
<cfoutput query="ProjectsInfo">
<cfset ProjectID = project_sid />
<cfoutput><cfdump var="#project_sid# #ProjectID#, "></cfoutput>
<cfabort>
</cfoutput>
Output from cfdump:
123456789 123456789, 17 123456789, 18 123456789, 19 123456789, 23
123456789, 24 123456789, 25 123456789, 28 123456789, 29
123456789, 32 123456789, 40 123456789, 41 123456789, 42
123456789, 43 123456789, 50 123456789, 55 123456789, 56
123456789, 59 123456789, 60 123456789, 61 123456789, 63
123456789, 64 123456789, 65 123456789, 66 123456789, 67
123456789, 68 123456789, 128 123456789, 148 123456789, 168
123456789,
What's going on here? Why is ProjectID set once and remains constant
at 123456789? Thanks.
Kin Wong
<cfcomponent displayname="ApplicationPlugin"
extends="MachII.framework.Plugin"
hint="This acts like application.cfm for a regular ColdFusion
application.">
<cffunction name="configure" access="public" returntype="void"
output="false">
<cfset VARIABLES.pm = getAppManager().getPropertyManager()/>
<cfset VARIABLES.userSessionFacade = createObject("component", "RMIT
Mach II.model.UserSessionFacade")/>
<cfset VARIABLES.pm.setProperty("userSession", userSessionFacade)/>
</cffunction>
</cfcomponent>
My question is why isn't ProductID change as I loop through the
<cfoutput query="ProjectInfo>? It retains the value from the first
assignment. Am I missing something with the way var-declared
variables work in functions and components? I thought that by var-
declaring a variable ensures that the variable is destroyed after the
function call, which is what I want. But it seems like within a
function, you cannot re-assign it.
<cfoutput query="ProjectsInfo">
<cfset ProjectID = project_sid />
<cfoutput><cfdump var="#project_sid#
#ProjectID#, "></cfoutput>
<cfabort>
</cfoutput>
ProjectID and ProjectsInfo are var-declared at the top of the
function.
Output from cfdump:
123456789 123456789, 17 123456789, 18 123456789, 19 123456789, 23
123456789, 24 123456789, 25 123456789, 28 123456789, 29
123456789, 32 123456789, 40 123456789, 41 123456789, 42
123456789, 43 123456789, 50 123456789, 55 123456789, 56
123456789, 59 123456789, 60 123456789, 61 123456789, 63
123456789, 64 123456789, 65 123456789, 66 123456789, 67
123456789, 68 123456789, 128 123456789, 148 123456789, 168
123456789,
<!--- outer loop --->
<cfoutput query="ProjectsInfo">
<!--- ProjectID = project_sid from record 1 of ProjectsInfo --->
<cfset ProjectID = project_sid />
<!--- inner loop --->
<cfoutput>
<!--- dump inner loop records, one at a time --->
<cfdump var="#project_sid# #ProjectID#, ">
</cfoutput>
<!---
break from outer loop;
you never proceed to record 2 of ProjectsInfo
and ProjectID is never updated.
--->
<cfabort>
</cfoutput>
This is basically what you've done:
<cfset ProjectID = ProjectsInfo.project_sid[1] />
<cfoutput query="ProjectsInfo">
<cfdump var="#ProjectsInfo.project_sid# #ProjectID#, ">
</cfoutput>
If you put the <cfoutput> around the <cfdump> just to output the
contents of the <cfdump>, then remove it. <cfdump> doesn't need
<cfoutput> to work.
HTH,
Adrian