0

我正在尝试创建类似的嵌套结构

<cffunction name="setDataAllWithFilter" output="false" access="public">

    <cfargument name="stCollection" required="true" type="Struct" />

    <cfif NOT StructKeyExists( Session, this.LOCAL ) >
        <cfset Session[this.LOCAL] = StructNew() />
    </cfif>

    <cfif NOT StructKeyExists( Session[this.LOCAL], "Data" ) >
        <cfset Session[this.LOCAL]["Data"] = StructNew() />
    </cfif>

    <cfif NOT StructKeyExists( Session[this.LOCAL]["Data"], "Filtered" ) >
        <cfset Session[this.LOCAL]["Data"]["Filtered"]  = StructNew() />
    </cfif>

    <cfreturn SetAll( Arguments.stCollection, Session[this.LOCAL]["Data"]["Filtered"] ) />

</cffunction>

这样可以吗?或者有更好的方法吗?

谢谢

4

3 回答 3

1

您可以查看使用StructAppend()来设置会话结构:

<cfscript>
initStruct = {Data={Filtered={}}};
StructAppend(session[this.local],initStruct,false);
</cfscript>

不是我还没有时间在这里测试这个,所以ymmv

于 2012-05-24T16:00:01.307 回答
1

SetVariable 函数将创建嵌套结构以满足要求。

 <cfset SetVariable("test1.test2.test3",4)>

将创建一个等于 4 的变量 test1["test2"]["Test3"]。

还要查看 StructGet ,它允许您基于路径(字符串)创建空结构。

于 2012-05-24T16:22:34.840 回答
0

你在做什么很好。您可以使用 cfparam 让您的生活更轻松

<cffunction name="setDataAllWithFilter" output="false" access="public">

    <cfargument name="stCollection" required="true" type="Struct" />

    <cfparam name="session[this.local]" default="#structNew()#">
    <cfparam name="session[this.local].Data" default="#structNew()#">
    <cfparam name="session[this.local].Data.Filtered" default="#structNew()#">

    <cfreturn SetAll( Arguments.stCollection, Session[this.LOCAL]["Data"]["Filtered"] ) />

</cffunction>
于 2012-05-24T20:04:52.207 回答