Loading ColdBox Settings from the database

Tips & Tricks , Walkthroughs Add comments
By: Luis Majano
The ColdBox configuration file has a place for you as the developer to create as many custom setting variables as you like.  You can create simple or complex settings via JSON notation.  This approach is great, but sometimes, you want for these variables to be, well, more dynamic.  The following UDF helps you achieve this, by storing your name-value pairs of settings in a persisten storage such as a database.  Then just basically call this UDF from your application start handler and voila!! You are ready to roll with settings that can be managed from a control panel of your application or any other dynamic approach.  You can even get creative and create your own reloading techniques for only application settings. <cffunction name="loadOptions" access="private" returntype="void" hint="" output="false" >
   <cfset var qOptions = 0>
   <cfset var jsonRegex = "^(\{|\[)(.)*(\}|\])$">
   <cfset var oUtilities = getPlugin("Utilities")>
   <cfset var thisValue = "">
   <cfset var oJSON = getPlugin("json")>
   
   <!--- Get Options From the DB, my case is the SiteManager Dependency --->
   <cfset qOptions = getSiteManager().getOptions()>
   
   <!--- Loop --->
   <cfloop query="qOptions">
      <cfset thisValue = oUtilities.placeHolderReplacer(trim(qOptions.value),getSettingStructure())>
      <cfif reFindNocase(jsonRegex,thisValue)>
         <cfset setSetting(qOptions.name, oJSON.decode(replace(thisValue,"'","""","all")) )>
      <cfelse>
         <cfset setSetting(qOptions.name, thisValue)>
      </cfif>
   </cfloop>
</cffunction>
There you go!!

0 responses to “Loading ColdBox Settings from the database”

Leave a Reply