Copy link to clipboard
Copied
hi guys,
my problem: need to set a datasource in application.cfm so I do : <cfset mydatasource = "hello">
application.cfm is in the root folder. my components are in cf_components folder and if I try to use :
<cfquery name="getMe" datasource="#mydatasource#">
.....
</cfquery>
so all in all, i get an error that variable mydatasource is not defined.
any help much appreciated.
cheers,
Simon
Other options:
1. Put your mydatasource variable into a scope that is visible to your components, request or application scopes could be used for this.Replace mydatasource with either application.mydatasource or request.mydatasourceAbout scopes: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fd5.htmlCopy link to clipboard
Copied
Option ONE
Move Application.cfm to a higer directory that is a parent to both the roor foder and the cf_components folder so that it applies to both.
Option TWO
Put another Application.cfm file in the cf_components folder to control the code in that folder. You either just copy the root one, but that would mean there are now two you have to maintain. Or you could just <cfinclude....> that other Application.cfm file. That would probably require a mapping for one directory to access the other, IIRC.
Hope that helps.
Copy link to clipboard
Copied
Other options:
1. Put your mydatasource variable into a scope that is visible to your components, request or application scopes could be used for this.Replace mydatasource with either application.mydatasource or request.mydatasourceAbout scopes: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fd5.htmlCopy link to clipboard
Copied
JR "Bob" Dobbs wrote:
1. Put your mydatasource variable into a scope that is visible to your components, request or application scopes could be used for this.Replace mydatasource with either application.mydatasource or request.mydatasource
As Adam pointed out, request scope is may not be accessible to functions inside the CFC and a variable inside of the application scope is not going to be the same, unless you have done something to ensure that both code files are running under the same applicaiton definition, I.E. Application.name string which is usually defined in that Application.cfm|cfc file. Thus both directories will somehow or the other need to be sharing the same or similar Applicaiton.cfm|cfc file OR in some other way account for this.
JR "Bob" Dobbs wrote:
2. Specify a default datasource using the datasource attribute of CFAPPLICATION. This requires CF9.
It also requires that you change to using an Application.cfc file rather then the Application.cfm file that you mentioned using.
Copy link to clipboard
Copied
thanks guys, i've used application.mydatasource and it worked a charm. luv this forum as rapid response is amazing!
Copy link to clipboard
Copied
Variables in the variables scope of you mainline templates are not exposed to code within CFCs. Others have suggested setting the DSN into the application or request scope, but even if you do that I would still not access it directly from your CFC methods, I'd pass it into your init() method and within init(), set it into the variables scope for subsequent method calls to use. If you only use transient objects, pass the value in as an argument to each method.
--
Adam