cffunction setting variables

Posted on 2006-06-01
Last Modified: 2013-12-24
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#" ?

Question by:bhinshawnc1
    LVL 7

    Assisted Solution

    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.
    LVL 10

    Assisted Solution

    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#">
    LVL 18

    Accepted Solution


    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.

    LVL 3

    Author Comment

    All helpful responses. Extra proints to bwasyliuk for the first to respond

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
    When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now