问题
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?
回答1:
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.
回答2:
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>
来源:https://stackoverflow.com/questions/39002299/application-cfc-setting-dsn-and-calling-that-dsn