config.xml and Datasource

Hi Luis,

I can't use getDatasource("name") in my model. All the calls to the
database are in my models. Switching the database in the config.xml
would be very handy, but without access to this information in my
model it is quite difficult. How would you do this?

best regards,

Is that for databases name difference between production and development? If
so check out the environment interceptor.


As Andrew mention about environment interceptor implementation.
I would suggest please read this doc.

Please let us know if you require more help for this.



I use setColdBox and getColdBox in my model cfc's and a function like this:

   <cffunction name="getDsn" access="public" returntype="string">
      <cfreturn getColdBox().getSetting('datasources')>

Take a look at the Security Interceptor Sample, then you know what I mean.


Hi Everybody,

thanks for your suggestions. My Intention was to copy an application
and rename the database, so that I don't have to do it in all my
models. I don't know if enviroment control will hit this need. It
seems more for development purpose. Ernst suggestion seems to be
right. I just have to figure out what getColdBox() is, as it doesn't
seem to be available out of the box in handlers and models.

take care,

I'm using you could have something like this:

   var cfcModelRoot = getController().getSetting('cfc.model.root');
   // Security Manager
   addConstructorProperty("securityManager","coldbox",getController() );

Again, take a look at the Security interceptor example...


So you can have a Base.cfc which your manager extends..

Your Base.cfc could look like this:

<cfcomponent output="false">

  <cffunction name="init" access="public" returntype="struct" output="false">
    <cfargument name="transfer" type="any" required="yes">
    <cfargument name="coldBox" type="any" required="yes">
    <cfset setTransfer(arguments.transfer)>
    <cfset setColdBox(arguments.coldBox)>
    <cfreturn this>

  <cffunction name="setTransfer" access="private" returntype="void">
    <cfargument name="transfer" type="any" required="yes">
    <cfset variables.transfer = arguments.transfer>

  <cffunction name="getTransfer" access="public" returntype="any">
    <cfreturn variables.transfer>

  <cffunction name="setColdBox" access="private" returntype="void">
    <cfargument name="coldbox" type="any" required="yes">
    <cfset variables.coldBox = arguments.coldBox>

  <cffunction name="getColdBox" access="public" returntype="any">
    <cfreturn variables.coldBox>

  <cffunction name="getDsn" access="public" returntype="string">
    <cfreturn getColdBox().getSetting('datasources')>

  <cffunction name="getManager" access="package" returntype="any">
    <cfargument name="componentName" type="string">
    <cfset var manager =
    <cfreturn manager>

Yes Environment interceptor is what you're looking for.

Assign settings based on development, testing/qa, UAT, production.

However read the documentation when it comes to the database.

As for getting things into your model, it all depends how you are MANAGING the model. What do I mean by managing? Well, are you using coldspring or lightwire for dependency injection or are you creating all of your model objects by hand. This is very important in order to choose an implementation path.

What are you using at the moment?

If you are doing things manually, I suspect you are doing this in the Application Start Handler, then you can there call framework methods, to inject either objects or structures into your model objects.

Let us know.

Hi Luis,

at the moment I'm not using coldspring or lightwire. Therefore I do
everything manually, I guess. Application Start would be a good place
then to set the DSN. Can you tell me which scope I should use for
variables (e.g. #request.dsn# ) or structures to have access in my
models to them?



I suggest using the cache or session/application storage plugins for persistence. Try not to access scopes from your handlers.