Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

set datasource in application.cfm

Community Beginner ,
Aug 31, 2010 Aug 31, 2010

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

3.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Aug 31, 2010 Aug 31, 2010

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

About scopes: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fd5.html


2. Specify a default datasource using the datasource attribute of CFAPPLICATION.  This requires CF9.

CFAPPLICATION: http://help.adobe.com/en_US/ColdFusion/...
Translate
Valorous Hero ,
Aug 31, 2010 Aug 31, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 31, 2010 Aug 31, 2010

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

About scopes: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fd5.html


2. Specify a default datasource using the datasource attribute of CFAPPLICATION.  This requires CF9.

CFAPPLICATION: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d69.html

CFQUERY: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fae.html
Note that "ColdFusion 9: Datasource attribute is optional now."
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 31, 2010 Aug 31, 2010

JR &quot;Bob&quot; 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 &quot;Bob&quot; 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 31, 2010 Aug 31, 2010
LATEST

thanks guys, i've used application.mydatasource  and it worked a charm. luv this forum as rapid response is amazing!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 31, 2010 Aug 31, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources