Coldfusion中的递归函数 [英] Recursive Function in Coldfusion

查看:85
本文介绍了Coldfusion中的递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Coldfusion中创建一个递归函数,并且遇到了一些问题。

I'm trying to create a recursive function in coldfusion and am coming across some issues.

这是我的逻辑:

<cffunction name="getEvents" access="private">
  <cfargument name="strtdate">
  <cfargument name="parentID" default=0>
  <cfqeury name="qry" datasource="db">
    select *
    from table
    where parentid = #parentid# and 
          starttime between #strtdate# and #DateAdd('d', 1, strtdate)#
  </cfquery>

  <cfset events = arraynew(1)>
  <cfloop query="qry">
    <cfset events[qry.currentrow] = structnew()>
    <cfset events[qry.currentrow].id = qry.id>
    <cfset subevents = getEvents(strtdate, qry.id)>
    <cfif arraylen(subevents)>
      <cfset events[qry.currentrow].subevents = subevents>
    </cfif>
  </cfloop>

  <cfreturn events>
</cffunction>

问题在于,一旦函数调用自身,它将在循环中释放原始查询。现在,我将事件分为三层,但我不想一遍又一遍地编码相同的代码来处理所有事件。

The problem is that once the function calls itself once it looses the original query in the loop. I now the events are three level deep but I don't want to have to right up the same coded over and over to handle all the events.

我想最终得到一个结构数组,其中包含给定日期的所有事件和子事件。

I would like to end up with an array of structs that contains all the events and subevents for a given day.

推荐答案

尝试对查询对象进行 var 范围界定。实际上,通常应该使用适当的作用域。例如:

Try var scoping your query object. In fact you should use proper scoping in general. For example:

<cffunction name="getEvents" access="private">
  <cfargument name="strtdate">
  <cfargument name="parentID" default=0>

  <cfset var qry = "" />
  <cfset var events = "" />
  <!--- etc. --->


  <cfquery name="qry" datasource="db">
    select *
    from table
    where parentid = #parentid# and 
          starttime between #ARGUMENTS.strtdate# 
    and #DateAdd('d', 1, ARGUMENTS.strtdate)#
  </cfquery>

  ... etc.

否则,所有内容都会进入变量作用域并覆盖其他我怀疑的对象。

Otherwise everything is going into the VARIABLES scope and overwriting others I suspect.

希望有帮助!

PS:您还应该考虑在查询中使用< cfqueryparam />

PS: You should also consider using <cfqueryparam /> in your queries.

这篇关于Coldfusion中的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆