代码之家  ›  专栏  ›  技术社区  ›  Jas Panesar

从查询设置ColdFusion动态应用程序变量

  •  1
  • Jas Panesar  · 技术社区  · 16 年前

    我胡思乱想,想把我的应用程序变量移到一个可以在applicationstart上加载的表中会更好。

    我的目的是让ANT推出这个应用程序,并更改数据库和众所周知的presto中的一些设置。。

    在我的测试代码中,application.cfc有一个简单的查询来调用所有变量名,然后有一个cfloop来将应用程序范围内的每个变量设置为application.varname。

    应用程序启动时未报告错误。。但是,尝试引用变量会产生未定义的类型错误。

    我的蜘蛛感觉告诉我这是很小很明显的。。。有什么想法吗?

    谢谢!!

    更新1:我看到的似乎是设置动态变量名,而它们是应用程序变量这一事实似乎没有多大影响。。

    http://www.bennadel.com/blog/152-Dynamic-ColdFusion-Variables-Via-Quoted-Naming.htm

    4 回复  |  直到 12 年前
        1
  •  6
  •   LeighLeigh    16 年前

    我不知道作者是在鼓吹这种语法,还是仅仅证明它是有效的,以此作为一个兴趣点。

    就我个人而言,我更喜欢数组表示法。我认为这有助于培养良好的范围界定习惯。

    <!--- array notation --->
    <cfset scope["staticName"& dynamicPortion] = "some value"> 
    
    <!--- example 1  --->
    <cfset variables["baseName"& x] = "oh brother">
    <!--- example 2  --->
    <cfset variables["baseName#x#"] = "oh brother">
    
        2
  •  4
  •   Jas Panesar    16 年前

    我的问题的答案是通过引用动态变量名。。

    <!--- Loop over the girls and alter the values. --->
    <cfloop index="intGirl" from="1" to="3">
    
    <!--- Randomly pick 1 (true) or 0 (false). --->
    <cfif RandRange( 0, 1 )>
    
    <!--- Set the dynamic variable naming used quoted evaluation. --->
    <cfset "Girl#intGirl#" = "super sexy" />
    
    </cfif>
    
    </cfloop>
    

    更多。。。

    http://www.bennadel.com/blog/152-Dynamic-ColdFusion-Variables-Via-Quoted-Naming.htm

        3
  •  3
  •   Chris Nava    16 年前

    使用cfloop+cfset而不是cfoutput

        4
  •  2
  •   Hooray Im Helping codaddict    16 年前

    如果我正确地阅读了您的问题,您是说您正在cfoutput标记中设置应用程序变量?

    你使用的cfoutput是

    <cfoutput query="queryName">
      <!--- Setting code in here --->
    </cfoutput>
    

    应该使用cfloop而不是cfoutput

    <cfloop query="queryName">
      <cfset application.varName = queryName.varName />
    </cfloop>
    

    不过,如果没有一些代码就很难帮上忙。

    我的问题是:如果要将应用程序变量转储回应用程序范围,那么为什么要首先将它们存储在数据库中?