[4.0] Syntax for DSN in custom settings for Dev env?

I hope the subject isn’t totally confusing.

In my Coldbox.cfc, I declared my three environments – local, dev and prod. I have three functions at the bottom named as such. The last line of each one, I added coldbox.dsn which is a copy of the line up in the datasources declaration. Obviously that’s the wrong syntax.

Example:

datasources = {

dsn = {name=“localdb”, dbType=“mysql”, username=“root”, password=“testing”}

};

// Local Environment

function local(){

if(structKeyExists(URL,‘fwreinit’))

ormReload();

//Override Settings

coldbox.debugmode=false;

coldbox.handlersIndexAutoReload = true;

coldbox.handlerCaching = false;

coldbox.eventCaching = false;

coldbox.reinitpassword = “”;

coldbox.debugpassword = “”;

wirebox.singletonreload = true;

modules.autoReload=true;

//Debugger Settings

debugger.showRCPanel = false;

//Custom Settings

coldbox.customErrorTemplate = “/coldbox/system/includes/BugReport.cfm”;

settings.show_debug=“true”;

coldbox.dsn = {name=“localdb”, dbType=“mysql”, username=“root”, password=“testing”};

So, what IS the correct way to do this?

Thanks!

Rob

environments

The configuration CFC has embedded environment control and detection built in and it is much more extensive than using the Environment Interceptor and the XML approach. In this structure you will setup the environments and their associated regular expressions for its cgi host names to match. If the framework matches the regex with the associated cgi.http_host of the startup request then it will set a setting called Environment in your configuration settings and will then go ahead and look for that environment setting name in your CFC as a method. That’s right, it will check if your CFC has a method with the same name as the environment and if it exists it will call it for you. Here is where you basically override, remove or add any settings according to your environment.

Important : The environment detection occurs AFTER the configure() method is called. Therefore, whatever settings or configurations you have on the configure() method will be stored first.

environments = {
	development = "^cf9.,^railo."
};

In the above example, I declare a development key with a value list of a regular expressions. So if I am in a host that starts with cf9 this will match and set the environment setting equal to development and then look for a development method in this CFC and execute it:

/**
* Executed whenever the development environment is detected
*/
function development(){
	// Override coldbox directives
	coldbox.handlerCaching = false;
	coldbox.eventCaching = false;
	coldbox.debugPassword = "";
	coldbox.reinitPassword = "";
	
	// Add dev only interceptors
	arrayAppend(interceptors, {class="coldbox.system.interceptors.ColdboxSidebar} );
}

Isn’t this cool? Just create a method with the same name of the environment and go for it, sure makes life simpler! But what if I have my own strategy on detecting my environment that doe not involve thecgi.http_host?

Custom Environment Detection

You then will do your own custom environment detection. You will NOT fill out an environments structure but actually create a method with the following signature:

string public detectEnvironment(){
}

Create a public method called detectEnvironment() that returns a string. If the framework detects that you have created this method, it will call it for you. You will then do your own custom environment detection and return the name of the environment you are on as a string. The framework will then save that string as the current environment setting and look for a method with that same name and execute it if it exists. So there you go, if you don’t like our basic approach, then override it and extend it.

Thanks, but that didn’t answer my question.

Rob, you don’t need a production() method-- your configure() method acts as that. You just need to override for non-production sites.

To answer your question-- the configure method simply creates structs in the variables scope. There are several structs created: coldbox, layouts, interceptors, datasources, etc. The datasources struct is not part of coldbox so you can just override bits like this:

// entire datasource shebang

datasources.dsn = {name=“localdb”, dbType=“mysql”, username=“root”, password=“testing”};

// one bit
datasources.dsn.username=“root”;

datasources.dsn.password=“testing”;

Also, this bit doesn’t belong in your config:

if(structKeyExists(URL,‘fwreinit’))

ormReload

I would put that in your Application.cfc’s onRequestStart() method. Perhaps a preProcess interceptor, but ideally you want that reloading before the framework does anything.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

BTW… while I’m at it… this is for up to v3.8.0, in my subject header I put [4.0].

While good info (I’ve seen it), it didn’t answer the specific question of how to include a custom DSN in there.

Also, for what it’s worth, “dsn” is the name of your data source. If you only have a single data source in your app, “dsn” may work for you, but I usually try to be a bit more descriptiive, or better yet, match the name of the actual CF data source. Then, if you need to add a second data source in the future, they will both be named something meaningful.

datasources = {
main = {},
reporting = {},
logs = {}
}

property name=“DSNMain” inject=“coldbox:datasource:main”;
property name=“DSNreporting” inject=“coldbox:datasource:reporting”;
property name=“DSNLogs” inject=“coldbox:datasource:logs”;

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks Brad!! That got it working great.

I love Coldbox but there are times when it leaves me feeling like a complete idiot. :slight_smile:

No worries. Sometimes I think it’s just the illusion of magic. The config file is actually really simple. It’s just creating ColdFusion structs in the variables scope, so the normal rules of CFML apply when it comes to creating, manipulating, or overriding them.

Also, for what it’s worth, here is the equivalent page that what Andrew pasted in in the “current” version of the docs:

http://coldbox.ortusbooks.com/content/configuration/coldboxcfc/configuration_directives/environments.html

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com