Coldfusion 中的递归函数

发布于 2024-09-25 00:18:59 字数 953 浏览 3 评论 0原文

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

这是我的逻辑:

<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>

问题是,一旦函数调用自身,一旦它在循环中丢失原始查询。我现在的事件是三层深度,但我不想一遍又一遍地使用相同的编码来处理所有事件。

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

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

Here is the logic I have:

<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-10-02 00:18:59

尝试 var 确定查询对象的范围。事实上,一般来说您应该使用正确的范围。例如:

<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.

否则,所有内容都会进入 VARIABLES 范围并覆盖我怀疑的其他范围。

希望有帮助!

PS:您还应该考虑在查询中使用

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.

Hope that helps!

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

救星 2024-10-02 00:18:59

确保对变量进行 var 作用域,以将它们保留在函数的每次调用中。

在您的函数中(如果在 CF8 或更早版本中,则位于顶部):

<cfset var qry = ''>
<cfset var events = ''>
<cfset var subevents = ''>

Make sure you var-scope your variables to keep them local to each invocation of the function.

In your function (at the top if in CF8 or earlier):

<cfset var qry = ''>
<cfset var events = ''>
<cfset var subevents = ''>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文