How to dynamically call a CFC function
-
14-07-2021 - |
Вопрос
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?
Решение
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>