data-structuresdynamiccoldfusionargumentscfc

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


Solution

  • 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....