overwrite DSN property

Hello.

I need to overwrite DSN property.

Currently I declare the <cfproperty name="dsn"
inject="coldbox:datasource:DSN" scope="instance" /> and it works just
fine. What I want to do next is for example I am running coldfusion
scheduled jobs I want to insert records to a different databases just
simply passing an argument.dsn .

how can this be done

Please advise

From where will you be passing the Dsn?

Luis Majano
CEO
Ortus Solutions, Corp
Toll free phone/fax: 1-888-557-8057
Mobile: 909-248-3408

www.coldbox.org

I will passing the DSN from the handler

For example , going from the handler to DAO file to insert record

Why don't you just create a plugin? Like:

<cfcomponent>
  <cfproperty name="dsn" inject="coldbox:datasource:DSN" />
  <cffunction name="getName" returntype="string">
    <cfargument name="datasource" required="true" default="" />
        <cfif arguments.datasource neq "">
      <cfreturn arguments.datasource>
    <cfelse>
      <cfreturn dsn.getName()>
    </cfif>
  </cffunction>
</cfcomponent>

Then, instead of:
<cfproperty name="dsn" inject="coldbox:datasource:DSN" scope="instance" />
do
<cfproperty name="dsn" inject="myPlugin:datasource" scope="instance" />

That way, you can either do dsn.getName() (which will return the datasource
dsn from the bean, or dns.getName("bleh") to pseudo set the datasource...

Well I already have a 100+ places where this will need to be changed.
But I like this approach

The other option would be to set the name, but then revert back somehow once
you're done, so

<cfset application.ds = dsn.getName()>
<cfset dsn.setName("bleh")>

Once you're done....

<cfset dsn.setName(application.ds)>

Tom’s approach is a valid one. But, I’d use the request scope instead of the application scope.

You’d need to change your references from injecting the DSN info to accessing the request scope.

I did all this but ca not detect arguments.dsn to be passed to the custom plugin

and then

<cfif structKeyExists(arguments,“DSN”)>
<cfif Len(trim(arguments.dsn))>

<cfreturn instance.dsn.getName()>

<cfreturn instance.dsn.getName()>

That’s because there’s no arugment in the fuction J

even when i add argument I get empty arguments nowing that I passing the argument.

How do I add the argument?

Thats is how i call my plugin

even when i add argument I get empty arguments nowing that I passing the argument.

How do I add the argument?

Thats is how i call my plugin

So when I get to my model files I do pass DSN as an argument, but when I try to inject myPlugin I need to pass dsn to that plugin

This is what I have:

PLUGIN:

<cfif arguments.name neq “”>

<cfreturn dsn.getName()>

MODEL:

……

Useage:

dsn.getName() // returns coldbox datasource

dsn.getName(“mydatasource”) // returns your datasource

Well I get that. But what if I need the same model file to run and do not pass the dsn argument it will cause an error message

is init() function executes by itself or it need to callled. What i am thinking is to have init() and set default argument.dsn so if I pass something that the value other wise do not pass it

No it will return the default datasource:

dsn.getName() // returns coldbox datasource
dsn.getName(“mydatasource”) // returns your datasource

I’m unclear why you’d ever get any error messages with what I’ve just provided.

In one case i will supply dsn to the model , in another I will not .

I hate to ask a stupid question but I will anyway.

Is it:
(What you wrote)

OR

I still get variable dsn is undefined. I cannot inject anything into my apps. THis is frustrating. I must have something missing.