2

I used CFBuilder "create CFC" plugin to create a services for a table, so i could play around with OOD. Now I am struggling to use the "update" function generated in a dynamic manner.

I call a cfc, to get the structure of the account, passing an ID.

<cfinvoke component="cfc.Account.accountService" method="getAccount" returnvariable="Account" AccountID="#session.auth.AccountID#">

I could call the update function using a manual bit of code.

<cfset Account.setAccountFirstname('#form.AccountFirstname#')>

That works fine, but I want to dynamically update the structure based on data from a form. So I figured loop the fields in the form and produce the following

<!--- Dynanic call of submitted fields --->
<cfloop list="#form.FieldNames#" index="i">
    <cfset Account.set[i]('#Evaluate('#i#')#')>
    </cfloop>

Now of course that does not work! any ideas what would work? Or a better way to handle it?

  • 2
    Change `'#Evaluate('#i#')#'` to `form[i]` – Peter Boughton Oct 27 '12 at 13:39
  • Just re-read your actual question - see http://stackoverflow.com/questions/12631347/coldfusion-9-dynamic-method-call – Peter Boughton Oct 27 '12 at 13:50
  • @PeterBoughton link was actually my question. In this case, I'm not sure it will help as I was specifically unable to use `invoke` due to lack of `cfscript` support for it. Plus I was also dynamically calling the methods **within** the object instance. Take a look at my answer below. – AlexP Oct 28 '12 at 12:15
  • I probably meant to use a different link - there was one that was just a simple "how to use cfinvoke" question which is what this question appears to be too? – Peter Boughton Oct 28 '12 at 14:03

1 Answers1

3

What you are trying to do with invoke wont work, this is because you are passing the attribute as a standalone component argument (I.e the class path) you need to pass in the object instance instead.

Edit to add:

<cfset account = new Account()/>
<cfset data = {
  accountId = session.auth.AccountID
}/>
<cfset fieldNames = listToArray(form.fieldNames)/>
<cfif ! arrayIsEmpty(fieldNames)>
  <cfloop array="#fieldNames#" index="fieldName">
    <cfinvoke 
      component="#account#"
      method="set#FieldName#" 
      returnVariable="methodResult" 
      argumentCollection="#data#"
    />
  </cfloop>
</cfif>
AlexP
  • 9,906
  • 1
  • 24
  • 43