Coldfusion 中的递归函数
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
var
确定查询对象的范围。事实上,一般来说您应该使用正确的范围。例如:否则,所有内容都会进入 VARIABLES 范围并覆盖我怀疑的其他范围。
希望有帮助!
PS:您还应该考虑在查询中使用
。Try
var
scoping your query object. In fact you should use proper scoping in general. For example: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.确保对变量进行 var 作用域,以将它们保留在函数的每次调用中。
在您的函数中(如果在 CF8 或更早版本中,则位于顶部):
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):