Erik_T
November 7, 2011, 3:45pm
1
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
Erik_T
November 7, 2011, 5:28pm
3
I will passing the DSN from the handler
Erik_T
November 7, 2011, 5:34pm
4
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...
Erik_T
November 7, 2011, 6:22pm
6
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)>
Aaron1
November 8, 2011, 1:21am
8
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.
Erik_T
February 28, 2012, 5:26pm
9
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
Erik_T
February 28, 2012, 5:33pm
11
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
Erik_T
February 28, 2012, 5:34pm
12
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
Erik_T
February 28, 2012, 5:45pm
13
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
Erik_T
February 28, 2012, 6:31pm
15
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
Erik_T
February 28, 2012, 6:33pm
16
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.
Erik_T
February 28, 2012, 6:49pm
18
In one case i will supply dsn to the model , in another I will not .
Kevin_F
February 29, 2012, 1:54pm
19
I hate to ask a stupid question but I will anyway.
Is it:
(What you wrote)
OR
Kevin_F
February 29, 2012, 2:04pm
20
I still get variable dsn is undefined. I cannot inject anything into my apps. THis is frustrating. I must have something missing.