Rob Dudley
Rob Dudley

Reputation: 110

Is it possible to dynamically populate a CFC with arguments?

The following code errors:

<cfdbinfo datasource="#Application.DSN#" name="getCols" type="columns" table="#this.tableName#">
<cftry>
  <cfquery name="getColumnDetails" dbtype="query">
    SELECT COLUMN_NAME,TYPE_NAME
    FROM getCols
    WHERE IS_PRIMARYKEY = 'NO'
  </cfquery>
  <cfcatch>
    <cfset this.ErrorState = true>
    <cfthrow message="General DB Error">
  </cfcatch>
</cftry>

<cfloop query="getColumnDetails">
  <cfargument name="#getColumnDetails.COLUMN_NAME#" displayName="values" type="Any" required="false" />
</cfloop>

but I would really like to know if it is possible to dynamically set the arguments for a CFC — or is it better to simply pass in a struct and deal with that?

Thanks
Rob

Upvotes: 1

Views: 196

Answers (2)

Adrian Lynch
Adrian Lynch

Reputation: 8494

Unlikely.

Two ways, as you said, don't define the cfargument tags and instead look for them being passed in with StructKeyExists(ARGUMENTS, aDynamicName) or, create a code generator and write these methods to a file.

Upvotes: 1

marc esher
marc esher

Reputation: 4921

One way I've tried to do similar things to what you're doing is something along these lines:

<cffunction name="doSomethingWithDatabase">
<cfargument name="potentialColumns" type="string">
<cfargument name="columnValues" type="struct">

and then loop over the list of potential columns, using each element in the list as the index to search for in the columnValues struct. if that value exists in the struct, then yo'ure good; otherwise, you ignore that column in the update.

you'd then call the function something like this:

to get the columns you're looking for

alternately, you could ignore the potentialColumns argument and just get that information in your cfc:

<cffunction name="doSomethingWithDatabase">
<cfargument name="columnValues" type="struct">
<cfset potentialColumns = getMyColumns()>
.... loop....

Upvotes: 0

Related Questions