如何从一个cfc文件中的函数查询中调用另一个CFC文件中的函数?

时间:2016-06-10 21:23:45

标签: sql methods coldfusion invoke

我有一个具有多个功能的cfc文件(info.cfc),如下所示。

   <ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" events="map.events">
     <ui-gmap-marker ng-repeat="m in map.markers" coords="m.coords" icon="m.icon" idkey="m.id"></ui-gmap-marker>
   </ui-gmap-google-map>

另一个cfc文件(DateFunctions.cfc)包含一个带有两个参数的函数并返回一个日期。 DateFunctions.cfc文件如下:

<cfcomponent output="true" extends="DateFunctions">
    <cffunction name="getStatuses" access="remote" returntype="any" output="true" returnformat="plain">
       ...
    </cffunction>

    <cffunction name="viewDate" access="remote" returntype="any" output="true" returnformat="plain">
        <cfquery  name="records">
              SELECT
                 dbo.tickets.Incident,
                 dbo.tickets.Start_Date,
                 dbo.tickets.Days_Due
              FROM
                 dbo.tickets    
        </cfquery>
    </cffunction>
</component>

问题:如何从(info.cfc)中的查询中调用“addBusinessDays”,同时产生另一列结果。

我以为我可以做类似的事情:

<cfcomponent output="true" name="DateFunctions"">
    <cffunction name="addBusinessDays" access="remote" returntype="any" output="true" returnformat="plain">
       <cfargument name="daysToAdd" 
                required="yes" 
                type="numeric" 
                hint="The number of whole business days to add or subtract from the given date">
        <cfargument name="date" 
                required="No" 
                type="date" 
                hint="The date object to start counting from.." 
                default="#NowDateTime#">

         ...
         ... <!--- Perform some tasks --->

         <cfreturn Date>
    </cffunction>
</cfcomponent>

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作,但需要对循环进行额外处理。

编辑:根据下面的讨论,更新cfoutput到cfloop

 <cffunction name="viewDate" access="remote" returntype="any" output="true" returnformat="plain">
    <cfquery  name="records">
          SELECT
             dbo.tickets.Incident,
             dbo.tickets.Start_Date,
             dbo.tickets.Days_Due, 
            '' as Due_DATE
          FROM
             dbo.tickets    
    </cfquery>

    <cfset df = createobject("component","DateFunctions")>

    <cfloop query="records">
        <cfset records.Due_DATE = df.addBusinessDays(Days_Due, Start_Date)>
    </cfloop>

   <cfreturn records>
</cffunction>

答案 1 :(得分:0)

它不应该创建df对象,而应该绑定到this范围。这样,对象只创建一次。这应该使viewDate函数运行得更快,因为创建df没有任何开销。

此外,new只是一种更清晰的语法

<cfcomponent>

    <cfset this.df =  new DateFunctions()>

    <cffunction name="viewDate" access="remote" returntype="any" output="false" returnformat="plain">
        <cfquery  name="Local.records">
           SELECT
                dbo.tickets.Incident,
                dbo.tickets.Start_Date,
                dbo.tickets.Days_Due, 
                CAST(NULL AS datetime)
           FROM
                dbo.tickets    
       </cfquery>


      <cfloop query="Local.records">
         <cfset Local.records.Due_DATE = this.df.addBusinessDays( Local.records.Days_Due, Local.records.Start_Date)>
      </cfloop>

      <cfreturn Local.records>
    </cffunction>

<cfcomponent>