Sweet Little Snippet: Query to Arguments
I have a CFC method with one argument that is an incoming, single-row query:
This method will call another internal method X in order to perform some work, but method X will be looking for the query data as arguments (necessary because method X is also used by other methods who will pass in discrete argument values).
So, I need to "convert" my 'remoteData' argument to actual argument values before I call the other method.
Pretty simple, but not necessarily so straightforward when having to treat queries as a structure of arrays, so thought I'd share it.
As an aside, I went ahead and toyed with converting an entire query to a structure dynamically, too. Following is the snippet I used:
Pretty cool. Here are the dumps:
Doug out.
<cfargument NAME="remoteData" TYPE="query" REQUIRED="yes" DISPLAYNAME="remoteData" HINT="I am the query returned (and formatted) from a remote data store." />
This method will call another internal method X in order to perform some work, but method X will be looking for the query data as arguments (necessary because method X is also used by other methods who will pass in discrete argument values).
So, I need to "convert" my 'remoteData' argument to actual argument values before I call the other method.
<!--- add our remoteData values to the arguments... --->
<cfloop list = "#arguments.remoteData.columnlist#" index="f">
<cfset arguments[f] = remoteData[f][1] />
</cfloop>
<cfset myResults = methodX(argumentcollection = arguments) />
<cfreturn myResults />
<cfloop list = "#arguments.remoteData.columnlist#" index="f">
<cfset arguments[f] = remoteData[f][1] />
</cfloop>
<cfset myResults = methodX(argumentcollection = arguments) />
<cfreturn myResults />
Pretty simple, but not necessarily so straightforward when having to treat queries as a structure of arrays, so thought I'd share it.
As an aside, I went ahead and toyed with converting an entire query to a structure dynamically, too. Following is the snippet I used:
<!--- create a query to play with using querysim... --->
<cf_querysim>
UserInfo
userID,firstName,lastName,userGroups
100|Stan|Cox|33
200|Joe|Blow|35
</cf_querysim>
<cfset stArgs = structnew() />
<!--- put the query to a structure... --->
<cfoutput query="UserInfo">
<cfset thisKey = "Record" & UserInfo.currentrow />
<cfloop list = "#UserInfo.columnlist#" index="f">
<cfset stArgs[thisKey][f] = UserInfo[f][UserInfo.currentrow] />
</cfloop>
</cfoutput>
<cfdump var="#UserInfo#">
<cfdump var="#stArgs#" />
<cf_querysim>
UserInfo
userID,firstName,lastName,userGroups
100|Stan|Cox|33
200|Joe|Blow|35
</cf_querysim>
<cfset stArgs = structnew() />
<!--- put the query to a structure... --->
<cfoutput query="UserInfo">
<cfset thisKey = "Record" & UserInfo.currentrow />
<cfloop list = "#UserInfo.columnlist#" index="f">
<cfset stArgs[thisKey][f] = UserInfo[f][UserInfo.currentrow] />
</cfloop>
</cfoutput>
<cfdump var="#UserInfo#">
<cfdump var="#stArgs#" />
Pretty cool. Here are the dumps:
Doug out.
Subscription Options
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
Re: Sweet Little Snippet: Query to Arguments
Pretty good generic solution, but I think the code would be more self-documenting if you hardcoded the columns that you were passing as arguments.
It would read a lot easier if you passed
ItemID, CustID instead of
remoteData[f][1]
(Second attempt to post - first was blocked by Anti-spam key)
It would read a lot easier if you passed
ItemID, CustID instead of
remoteData[f][1]
(Second attempt to post - first was blocked by Anti-spam key)
Posted by Phillip Senn on March 2, 2007 at 11:29 AM
Re: Sweet Little Snippet: Query to Arguments
a bad job
Posted by Abel on May 25, 2007 at 7:16 AM