1

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?

Miguel-F
  • 13,450
  • 6
  • 38
  • 63
  • 1
    I thought you just called it out the same way. `datasource="krl"` from your example. What happened when you tried it? I think if you only define one `datasource` then you don't even have to specify that in the `cfquery` tag.`this.datasource` makes it the default for your application. – Miguel-F Aug 17 '16 at 17:53
  • 1
    In CF 9, you set the default datasource with this.datasource, like you did. Now your queries do not need to specify a datasource. – Evik James Aug 17 '16 at 18:37
  • *How do I call it out in my queries?* You don't. The purpose of establishing a default datasource is to avoid having to specify the DSN name in every cfquery ;-) As mentioned, you should omit the "datasource" attribute from your cfquery and the application will automatically use the default ie `this.datasource`. (Side note, [the `this` scope is not accessible outside the Application.cfc component](http://stackoverflow.com/a/23796613/104223).) – Leigh Aug 17 '16 at 22:35
  • @Miguel-F - That is really the answer. You should promote your comment to an "official" answer. – Leigh Aug 20 '16 at 03:28

2 Answers2

0

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.

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • 2
    `this.datasource` is a little different. It is an application setting which sets the default datasource to use for the application named in the Application.cfc file. With it defined you no longer have to use the `datasource` attribute of the `cfquery` tag. It will default to what you have assigned. It was added in CF 9 - [documentation](http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSd160b5fdf5100e8f790124b112a3b8b2adb-8000.html) – Miguel-F Aug 17 '16 at 20:04
-1

I am able to use this.datasource in any CFM page. Example:

<cfinsert tableName="#variables.type#s" dataSource="#this.datasource#">

Application.cfc looks something like this:

<cfcomponent
    displayname="Application"
    output="true"
    hint="Handle the application.">

    <!--- Set up the application. --->
    <cfset THIS.Name = "#cgi.server_name#" />
    <cfset THIS.SessionManagement = true />
    <cfset THIS.ApplicationTimeout = CreateTimeSpan( 1, 0, 0, 0 ) />
    <cfset THIS.SessionTimeout = CreateTimeSpan( 0, 0, 30, 0 ) />
    <cfset THIS.SetClientCookies = true />
    <!--- FOR THE DATASOURCE --->
    <cfset this.datasource = "MyDSN" />
...
</cfcomponent>

However in regular tags you don't need to specify the datasource at all, if it's in the THIS scope:

<cfquery name="get">
SELECT id
FROM restaurants
WHERE email = '#something#'
UNION       
SELECT id
FROM individuals
WHERE email = '#something#'
</cfquery>
Jules
  • 1,941
  • 15
  • 18
  • The circumstances under which `this` settings are accessible was explained on the [other thread](http://stackoverflow.com/a/23796613): *For the other settings in "this", they appear to be accessible from your pages because the pages are included into the application.cfc execution cycle by the OnRequest() method.* But as everyone has noted, it is not necessary. Side note, use of `cfinsert` has long been discouraged, primarily for security reasons. – Leigh Aug 22 '16 at 12:30
  • Oh I see. Rather than using would in the cfcomponent work? I believe the Request scope is accessible until the url us completely processed. – Jules Aug 22 '16 at 15:05
  • Not quite. When you use the optional `OnRequest` method, the included pages essentially run in the context of Application.cfc. That is why the page can access variables within the application's `this` scope. However, if you are *not* using `OnRequest`, that scope is not accessible, and attempts to use `this.datasource` will fail with an undefined error. If you really did need to access that type of variable, yes it could be placed in the `Request` scope. Though for things that seldom change, like a DSN, an application variable is probably more appropriate. – Leigh Aug 22 '16 at 15:21
  • Leigh - is it possible to contact you directly? – Jules Aug 22 '16 at 15:34
  • AFAIK, SO does not have any private IM/chat options. My email address is in my profile. – Leigh Aug 22 '16 at 15:55