Skip to main content
Participating Frequently
November 16, 2010
Question

Dynamically nameing a CFfunction in a loop

  • November 16, 2010
  • 2 replies
  • 1382 views

I want to do this

loop through query results

<cffunction name= "#loop.ID#_test">

     DO something with record of data from query

</cffunction>

now can I dynamically change the name of the cffunction ( and or Method)?
Can someone help me please

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    November 16, 2010

    Coldfusion wont allow you to name functions dynamically like that. Just as well. You can, in fact, design what you're trying like this:

    <cfloop>

    <cfset myArray = my_test_function(id_from_looping_over_query)>

    </cfloop>

    <cffunction name= "my_test_function">

         <cfargument name="id">

        <!---  DO something with record of data from query --->

    </cffunction>

    naylorrAuthor
    Participating Frequently
    November 16, 2010

    This might help get help
    this is what I have and it works but whenever I add something new it gets ver cumbersum


    <cfcomponent displayname="F_QU_004_harness" extends="mxunit.framework.TestCase">
        <cfobject component="setup" name="setupUtil">
       <!--- read xml file templatevariables.xml --->
        <CFFile action="READ" variable="xml" file="#ExpandPath('.')#\Templatevariables.xml">
        <cfset mydoc = XmlParse(xml)>
        <cfset MyDocArray =XmlSearch(mydoc,'SavedQuery')> 
        <!--- set up --->
        <cffunction name="setUp" returntype="void" access="public" output="false">
            <cfset this.selenium=setupUtil.setup()/>
           
        </cffunction>

        <!--- Shut down selenium --->
        <cffunction name="tearDown" returntype="void" access="public" output="false">
               <cfset setupUtil.tearDown()/>
        </cffunction>
        <!---set variables based on the index of the function --->
        <cffunction name="variablesetup" access="Private">
          <cfargument name="indexVar">
              <cfscript>
           
              this.queryId = #myDoc.xmlRoot.xmlChildren[indexVar]["queryId"].xmlText#;
                this.queryName=#myDoc.xmlRoot.xmlChildren[indexVar]["queryName"].xmlText#;
              this.UnqSQL =#myDoc.xmlRoot.xmlChildren[indexVar]["UniqueSQL"].xmlText#;
              this.AllSql=#myDoc.xmlRoot.xmlChildren[indexVar]["AllSQL"].xmlText#;
              </cfscript>
        </cffunction>


       
    <!--- the following are calls to the individual templates --->
        <cffunction name="All_Bio_Samples" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
    <!--- the number in the variablesetup call corresponds to the index in the xml file to the template--->
        <cfset variablesetup(1)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
         <cffunction name="Asthma_All" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(2)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="COPD_All" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(3)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Asthma_Adults" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(4)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Asthma_Kids" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(5)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="COPD_Under_50" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(6)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="ILD_All" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(7)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Sleep_Apnea" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(8)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Rhinitis_All" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(9)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Rhinitis_Adults" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(10)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Rhinitis_Kids" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(11)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Sinusitis_All" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(12)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Sinusitis_Adults" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(13)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Alpha_1_antitrypsin_Deficiency" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(14)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>
       
        <cffunction name="Sleep_Apnea_Kids" returntype="void" access="public" output="false">
          <cfset selenium=this.selenium/>
        <cfset variablesetup(15)>
          <cfinclude template="F_QI_004.cfm">
         </cffunction>

       
    </cfcomponent>

    I want to do this more dynamically like read the XML and let the data drive instead of ME doing all that typing of the same thing OVER AND OVER

    BKBK
    Community Expert
    Community Expert
    November 17, 2010

    Look at my previous post. Your last response came in a few minutes after mine, so I have to assume you hadn't read what I wrote.

    At the place where the component is instantiated, define two variables, say, category and setup_code. Category can take one of the values All_Bio_Samples, Asthma_All, ..., Sleep_Apnea_Kids. Setup_code can take one of the values 1, 2, ..., 15.

    This enables you to simplify your above code to:

    <cfcomponent>

    <cffunction name="processCategory" returntype="void" access="public" output="false">
    <cfargument name="category">
    <cfargument name="setup_code">
    <cfset selenium=this.selenium/>
    <cfset variablesetup(setup_code)>
    <cfinclude template="F_QI_004.cfm">
    </cffunction>

    </cfcomponent>

    ilssac
    Inspiring
    November 16, 2010

    Why would you want a different function for every row in the data.

    The normal way to handle this would be to create a function that does what you want done to each record, and then loop over the recordset and pass the data from each row into this function.

    <cffunction name="myRowFunc"...>

        <cfargument name="dataFromQuery">

    ....Do stuff with data from query ....

    ....Maybe I return information when I'm done.....

       <cfreturn retuneVar>

    </cffunction.

    Then you just call that in a query loop.


    <cfloop query="myRecordSet"...>

      <cfset aVar = myRowFunc(myRecordSet.data)>

       ....Do more stuff, maybe including information from the function call...

    </cfloop>

    naylorrAuthor
    Participating Frequently
    November 16, 2010

    They would be perfect but

    I am calling this function from mxunit and I need the cffunction name to change so I know what record of data failed in my testing.

    ilssac
    Inspiring
    November 16, 2010

    That may not be that easy to do and I can only think of one idea at this time and I'm doubtful it would really work.

    You are basically wanting to dynamically write CFML code, then execute that dynamic code.

    So try doing just that.

    Inside your query loop.

    1. Create a string of text that represents the CFML code to create the desired function.
    2. Save that string to a cfm text file.
    3. Include that text file in your code.
    4. Execute the function in the code.

    But I still think this is going to run afoul of compilations issues.  I.E. the template to create the functions will be complied to make the functions but the included functions will not be part of that compilation so the functions can not be created, but I don't know.