ColdFusion - Convert Query to CFC Setters

I am converting an old site into CF 10 and would like to transfer some of my helper code.

The code scans the request, finds the things that are in our instance, and fills them:

<cffunction name="populateSelf"> <cfargument name="source" type="query" required="yes" /> <cfif arguments.source.recordcount EQ 1> <cfloop list="#arguments.source.columnlist#" index="local.col"> <cfif structKeyExists(variables.instance, local.col)> <cfset variables.instance[local.col] = arguments.source[local.col]) /> </cfif> </cfloop> </cfif> <!--- one record? ---> </cffunction> 

I replaced structKeyExists(variables.instance, local.col) convenient evaluation of our current properties using 'getMetaData ()', but I am having problems with the following line: <cfset variables.instance[local.col] = arguments.source[local.col]) />

If I change it to <cfset this[local.col] =arguments.source[local.col] /> , it ignores the implicit setters and simply puts the results in this area ...

To try calling our setters, I tried this bit of code:

 <cfset setValue =arguments.source[local.col] /> <cfset evaluate("set#local.col#('#setValue#')" /> 

but this seems complicated and error prone (you should also avoid any "" in lines).

What is the best way to use a query to load some or all of the properties of CFCs without explicitly calling this.setPROPERTYNAME(query.COLUMN) , perhaps several dozen times?

+6
source share
2 answers

So, if I read all this correctly, your question is actually “how can I name a method dynamically?”, And everything else is asked in clothes?

You can use the line to set the name of the dynamic variable, then set the link to the function, then call the function through the link:

 myMethodName = "set#local.col#"; myMethodReference = this[myMethodName]; myMethodReference(arguments.source[local.col]); 
+7
source

If you want to call methods dynamically, you can use cfinvoke

 <cfinvoke method="set#property#"> 

Make sense?

+6
source

All Articles