variablescoldfusioncffunction

how to reference database variables inside a Coldfusion8 cffunction tag?


I'm doing my first steps in Coldfusion8. I managed to setup a component/service with a cffunction I'm calling.

Inside the function I need to contruct a 2x2 table with errors and corresponding error messages. Error messages are multi-language and stored in a MySQL table.

Problem:

I can't find a way to reference my variables from inside a CFFunction tag. This does not work:

// 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 I'm calling:

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

Inside errorMsg.cfc I'm doing:

<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>  

This just seems like an awful lot of code...


Solution

  • Just a best practices answer here. When referencing a variable you can just reference the name, no need to use ##.

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

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

    This doesn't solve your undefined problem, but is something you should follow moving forward (don't bother cleaning up old code), but this makes it much more readable imo.