且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我必须在Coldfusion中确定查询输出的作用域吗?

更新时间:2023-02-14 19:22:32

有人会告诉您***总是习惯范围调整,因为这可以使您避免在真正重要的地方犯下作用域错误。



个人认为,我喜欢在查询中使用cfoutput的方法且无需范围限制-在其他语言中等同于 WITH。由于查询将始终在查询驱动的cfoutput标记内的表单和url范围之前进行评估,因此在该实例中忽略范围不会产生任何问题。请记住,在CFC中,参数和局部作用域都是抢占式的-但这不是查询驱动的cfoutput的***位置-cfoutput是为方便显示 display 而设计的。 p>

但是,..其他人也会告诉你与众不同(还有一些热情:)。


If I'm a running a database query/stored procedure in Coldfusion, what is the proper way to reference fields returned from the query?

<cfstoredproc procedure="proc_select_extern" datasource="stokkers">
    <cfprocparam type="in" value="#Session.Extern#" cfsqltype="cf_sql_varchar" maxlength="13">
    <cfprocresult name="extern">
</cfstoredproc>
<cfoutput query="extern">
   <cfset variables.some = extern.foo>
   OR 
   <cfset variables.some = foo>
</cfouput>

Say extern includes foo, bar and foobar. Is it allowed and better to write:

 extern.foo;
 extern.bar;
 extern.foobar;

because I'm running through a page and often find these "naked" variables a little confusing to follow:

 foo;
 bar;
 foobar;

There is a lot of info on scopes and proper scoping but I have not found anything on query-output.

Thanks for clarification!

Some will tell you that it is good habitual practice to always scope because it keeps you from making scoping errors where it is really important.

Personally in views I like the approach of using cfoutput with a query and NOT having to scope - it's the equivalent of "WITH" in other languages. Since the query will always be evaluated before form and url scopes within a query driven cfoutput tag I do not see any issues with leaving off the scope in that instance. Keep in mind that in CFCs the "arguments" and local scope will both be preemptive - but that's not the best place for a query driven cfoutput - which is designed (ably designed) for convenient display.

But again.. others will tell you different (with some passion as well :) .