cffunction setting variables

Trying to modify some code that currently uses a settings.ini file to control the configuration. This won't work in final deployment environment.

Here's the current configuration

<cffunction name="getSettings" access="public" returnType="struct" output="false" hint="Returns application settings as a structure.">
<!--- load the settings from the ini file --->
            <cfset var settingsFile = replace(getDirectoryFromPath(getCurrentTemplatePath()),"\","/","all") & "/settings.ini">
            <cfset var iniData = getProfileSections(settingsFile)>
            <cfset var r = structNew()>
            <cfset var key = "">
            <cfloop index="key" list="#iniData.settings#">
                  <cfset r[key] = getProfileString(settingsFile,"settings",key)>
            <cfreturn r>

settings.ini contains:

I want to call the dsn via variables defined in root application.cfm, normally in a query I'd call them like:

<cfquery name="GetSurveys" datasource="#dbname#"  username="#dbuser#" password="#dbpw#"  >

In the functions, they were originally built like:

<cfquery name="GetSurveys" datasource="#variables.dsn#">

A sample function:
<cffunction name="getSurveys" access="public" returnType="query" output="false" hint="Returns all the surveys.">
      <cfargument name="bActiveOnly" type="boolean" required="false" default="false" hint="Restrict to active surveys only. Also does the date restriction.">
      <!--- testing <cfargument name="dbname" required="yes" type="string" hint="from root application.cfm"> --->
      <cfset var qGetSurveys = "">
      <cfset var survey = "">
      <cfquery name="qGetSurveys" datasource="#variables.dsn#">
      <!--- testing <cfquery name="qGetSurveys" datasource="#dbname#"  username="#dbuser#" password="#dbpw#"  >--->
            select      id, name, description, active, datebegin, dateend, resultmailto, surveypassword, thankyoumsg
            from      #variables.tableprefix#surveys
            <cfif arguments.bActiveOnly>
            where      active = 1
            and (datebegin is null or datebegin < <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">)
            and (dateend is null or dateend > <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">)
             <cfreturn qGetSurveys>

How do I take the function getsettings and set the variables it returns for DSN to those variables I've already defined in application.cfm for
datasource="#dbname#"  username="#dbuser#" password="#dbpw#"  so they are passed to the function getsurveys via datasource="#variables.dsn#" ?

Who is Participating?
PluckaConnect With a Mentor Commented:

Here's what we do

in application.cfm or application.cfc (CF7)

Just define your datasource in application scope


<cfset application.datasource = "DataSourceName" />

You can then access this variable everywhere, including in components and you don't need to pass it around every where.

bwasyliukConnect With a Mentor Commented:
I have done something similar with session variables, but I assume the same would work with application variables

              <!--- Set up User's Client system parameters --->
              <cfquery name="ClientConfig_lst" datasource="#session.dsn#">
              <cfloop query="ClientConfig_lst">
                <cfset "session.cc_#ClientConfig_lst.Col_Cd#" = ClientConfig_lst.ColValue>

Here I use a query to retrieve their settings, but you will call your function and then loop through your struct that is returned.

In the loop where I set the session.cc_... variables, you can set Application.<var_name> variables.

Let me know if you need me to expand further...

by the way, this relies on your function returning a struct with key values the same as what you want your variables to be called.
rob_lorentzConnect With a Mentor Commented:
you can either pass the structure returned by getSettings into getSurveys....
or call getSettings from within getSurveys....
or assign the structure returned by getSettings to a request scope variables like....
<cfset request.globalSettings = getSettings()>
then in getSurveys access them like....
<cfquery name="qGetSurveys" datasource="#request.globalSettings.dsn#">
bhinshawnc1Author Commented:
All helpful responses. Extra proints to bwasyliuk for the first to respond
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.