2

I'm attempting to build a method call from strings that have been passed into an object that refer to another object.

normally when calling an object we write the code like this:

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

However what I have done is created an array that contains the object name, the method name and the argument collection.

<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 recombine all this to be a method call.

UPDATE using suggestion but still with issue

While cfinvoke seems to work for:

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

it doesn't 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 the ColdFusion component or interface application.stObj.oNewsBusiness

Jarede
  • 3,310
  • 4
  • 44
  • 68

3 Answers3

7

CFInvoke is generally used to handle dynamic method calls.

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

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

Dan Wilson
  • 126
  • 5
  • I'm having issues with cfinvoke – Jarede Jun 14 '12 at 14:30
  • 2
    I'm guessing that's because you are looking for a reference already instantiated, not a component that needs to be instantiated from a path. Try this: – Dan Wilson Jun 14 '12 at 14:57
  • 1
    Got it. It looks like the object you really want is located inside of a struct (stObj) located in the application scope. So to make this work you can use: This will get the object out of the nested struct path based on the name (being dot delimited). However, this assumes the name will always have 2 dot delimited strings for the in-instance path. You can loop, if you may have more paths... – Dan Wilson Jun 14 '12 at 17:33
2

Dan is correct CFInvoke is the way to go

<cfinvoke component="#mycomponentname#" method="get"  arg1="#arg1#" arg2="#arg2#" arg3=..>
Tim Cunningham
  • 329
  • 1
  • 7
0
<cfinvoke component="application.#local.sObject#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>

from your update won't work because there are no # signs around 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>

There's probably an inline way of combining application. with the variable on the cfinvoke call, but I don't know off the top of my head.

Edit: Dan Wilson's comment does it better in an inline way.

Jason M
  • 510
  • 4
  • 11