Coldfusion 9 dynamic call method

I am trying to build a method call from strings that were passed to an object belonging to another object.

usually when calling an object, we write the code as follows:

application.stObj.oNewsBusiness.getNews(argumentCollection=local.stArgs); 

However, what I did is creating an array containing the name of the object, the name of the method, and a collection of arguments.

 <cfscript> local.stArgs = {}; local.stArgs.nNewsID = 19; local.stArgs.sAuthor = "John"; local.aData = []; local.aData[1] = local.stArgs; local.aData[2] = "stObj.oNewsBusiness"; local.aData[3] = "getNews"; </cfscript> 

however, I am struggling to re-arrange all this as a method call.

UPDATE using a sentence but still with a question

While cfinvoke seems to work for:

 <cfinvoke component="#application.stObj.oNewsBusiness#" method="#local.sMethod#" argumentcollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke> 

it does not work when doing something like:

 <cfscript> local.stArgs = local.aData[1]; local.sObject = local.aData[2]; local.sMethod = local.aData[3]; </cfscript> <cfinvoke component="application.#local.sObject#" method="#local.sMethod#" argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke> 

it generates an error:

Could not find ColdFusion component or application.stObj.oNewsBusiness interface

+2
source share
3 answers

CFInvoke is commonly used to handle calls to dynamic methods.

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7e0a.html

CFInvoke has an argumentscollection attribute, so you can pass your arguments the way you are used to.

+7
source

Dan right cfi nvoke is the way to go

 <cfinvoke component="#mycomponentname#" method="get" arg1="#arg1#" arg2="#arg2#" arg3=..> 
+2
source
 <cfinvoke component="application.#local.sObject#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke> 

from your update will not work because there are no # characters in the component variable.

You could do

 <cfset local.componentName = "application." & local.sObject> <cfinvoke component="#local.componentName#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke> 

Probably a built-in way to combine applications. with a variable to call cfinvoke, but I don’t know from my head.

Edit: Dan Wilson's comment makes it better in an embedded way.

0
source

Source: https://habr.com/ru/post/1436714/


All Articles