English 中文(简体)
• 如何在冷聚8电tag中参考数据库变量?
原标题:how to reference database variables inside a Coldfusion8 cffunction tag?

我在“冷聚8”上迈出了第一步。 我设计了一个部件/服务,有一台电灯。

在职能方面,我需要用错误和相应的错误信息再加2x2的表格。 错误信息是多语言的,储存在MySQL的表格中。

<<>Problem>:

我无法找到一种办法,在CFFunction tag中参考我的变量。 这并不可行:

// select from database
<cfparam name="Session.Sprache" default="DE">
    <cfquery datasource="iln" name="texte">
        SELECT *
        FROM languages
        WHERE sprache = "#Session.lang#"
     </cfquery>

<cffunction name="createErrMsgsLog" returntype="object">
    // create and populate 
    <cfset allErrMsgs=ArrayNew(2)>
    <cfset allErrMsgs[1][1]="firma">
    // not defined
    <cfset allErrMsgs[1][2]="#tx_validate_firma#">
    ....
 </cffunction>

Question:
How can I reference my variables aka #tx_validate_firma# correctly inside CFfunction. They are always undefined.

EDIT:
Ok. This seems to work:

Inside application.cfc 我叫:

<cfinvoke component="services.errorMsg"
     method="createErrMsgsLog"          
     returnvariable="errMsgs">
</cfinvoke>         

Inside errorMsg.cfc 我这样做了:

<cfcomponent displayname="errorMsg">    
    <cffunction name="createErrMsgsLog">
        <cfquery datasource="mine" name="texte">
            SELECT *
            FROM sprachen
            WHERE sprache = "#Session.Sprache#"
        </cfquery>
        <cfoutput query="texte">
            // column name = value
            <cfset "#trim(bezeichner)#" = "#trim(textinhalt)#">
        </cfoutput>
        // build array
        <cfset allErrMsgs=ArrayNew(2)>
        <cfset allErrMsgs[1][1] = "firma">
        <cfset allErrMsgs[1][2] = #tx_validate_firma#>
        ...

        <cfset errMsgs = serializeJSON(allErrMsgs)>
        <cfreturn errMsgs>
    </cffunction>
</cfcomponent>  

这似乎像许多法典......。

最佳回答

这里仅是最佳做法答案。 当提到变数时,你只能提到名字,不需要使用第2号。

For example <cfset "#trim(bezeichner)#" = "#trim(textinhalt)#">

Can be <cfset “#trim (bezeichner)#” = trim (textinhalt)>

这并没有解决你所未界定的问题,但这是你应该向前推进的一件事(don清旧法典),但这使它更加可读。

问题回答

To use a value inside a function, you need to pass the value into the function as an argument value, eg:

<cffunction name="createErrMsgsLog" returntype="object">
    <cfargument name="someName" [etc]>

然后将其称为论据。 该职能中的一些Name。

虽然您的can从内部范围和从主线进入变量上偏离了你的职能,但并不认为这样做特别好。

另一种考虑或许是,将您的职能从氟氯化碳档案的组成部分定义中总结出来,而不是仅仅在你的“主要”法典中加以界定。 这使得准则更加一致,使之更加可使用。 尽管如此,往往有理由对一次性职能进行在线界定。 但也许值得调查:

这里要思考的问题。 答案不像一些意见:

请问询中不要使用“选择 *”。 从我在此看到的评论中,tx_validate_firma被退回到点名文本中。 如果该守则明确指出:

<cfquery datasource="iln" name="texte">
    SELECT tx_validate_firma, etc
    FROM languages
    WHERE sprache = "#Session.lang#"
 </cfquery>

我们都知道价值来自哪里。 Oh,请看你在哪里条款中如何使用频率。

我假定这一工程是可行的,但似乎很奇怪:

<cfset allErrMsgs[1][2] = #tx_validate_firma#>

由于tx_validate_firma isn t specified,我们的外在者将使我们的头脑满为患,而有人可能试图在晚些时候维持这一局面。

<cfset allErrMsgs[1][2] = texte.tx_validate_firma>

Note that you don t need to use # in assignments like this. Generally within a ColdFusion tag you won t need them unless you are inside quotation marks. Something like this:

<cfset fullName = "#firstName# #middleInitial# #lastName#">




相关问题
JQuery AJAX .load - flash chart doesnt load in IE

An IE issue has me completely stumped. I have a coldfusion page that uses JQuery s AJAX .load function to load in a new flash file that is generated by coldFusion s cfchart tag. This works completely ...

Best Coldfusion Library for OpenID [closed]

I am getting ready to start a project that requires using OpenID within Coldfusion 8. I have found a number of different options and was wondering what has worked the best, get s the most support, ...

Find ColdFusion Generated ID

Is there a way to find the elements generated by ColdFusion s <CFLayout> and <CFLayoutArea> tags? These tags: <cflayout type="tab" name="MyAccount"> <cflayoutarea name="...

ColdFusion COM error

I am upgrading from CF4.5 to CF8. Calls to COM dll s that used to work in version 4.5 now throw a "Complex object types cannot be converted to simple values.." error. The COM object has a few arrays ...

What s the best way to write engine-specific CFML code?

Sometimes it is necessary to write different code for Adobe ColdFusion vs Railo vs OpenBD, due to differences in implementation. Do people have a specific method that they use for this? For example, ...

热门标签