coldfusionapplication.cfc

Application.cfc setting DSN and calling that DSN


I have been shown how to do this with Application.cfc instead of using the Application.cfm - that is fine, I like learning new stuff. Yet after I made the change I cannot figure out how to get the DSN working properly. Before I just used a set DSN in the Application.cfm file.

<cfparam name="DSN" default="">
<cfset DSN = "krl" />

And called it out here:

<CFQUERY NAME="Inital" DATASOURCE="#DSN#">
    SELECT Website_Name
    FROM InitalizationData
</CFQUERY>

Now setting it like:

component {
    this.name = "app"; 
    this.Sessionmanagement = true;
    this.datasource = "krl";

    public void function onSessionStart() {
        // initialize cart 
        session.cart = [];
    }
}

How do I call it out in my queries?


Solution

  • Inside Application.cfc, you would usually add a function onApplicationStart(). Then, inside that function define

    application.dsn = "foo";

    And reference it like so:

    <cfquery name="test" datasource="#application.dsn#">

    When you define the variable as this.datasource inside a CFC, the this scope exists only in the context of that CFC. It's not accessible from outside it.