• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfc application variables

Participant ,
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

Am I able to read application variables from with in a CFC? If so, how?

Code Snip:
<cfcomponent>
<cffunction name="function1" access="public" returntype="numeric">
<cfquery datasource="#data#" name="membersearch" maxrows="1">

Error:
Element DATA is undefined in APPLICATION.
TOPICS
Advanced techniques

Views

524

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

in theory you can, but you shouldn't. CFCs should be as unaware of the world outside of themselves as possible.

pass in the datasource as an argument to the function.

just out of curiousity...are you sure that's the line the error is on? you didn't scope #data# explicitly, and i was under the impression that the application scope was -not- one of the scopes that CF looks in for unscoped variables.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

application doesn't require scope, although I have started to scope them lately. I just missed that one. But back to my original question, I wasn't able to use the application data with or without the scope, so I just hardcoded the dsn. From what you just said, this seems as if it should best practice.

Thanks for the reply.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

hard coding isn't really the best practice.

you can go one of two routes to achieve "best practice" (with the caveat that "best practice" is a subjective term):

1) pass the dsn in as an argument. so you'd have:
<cfset foo = createObject('component', 'CFC.myCFC') />
<cfset bar = foo.function1(dsn="#application.DSN#") />
that's the quickest and easiest to address the issue at hand.

2) create an init() method of the CFC, and instantiate the DSN into the CFC's local scope which is available to all methods in the CFC. Still requires the DSN be passed in when the init() method is called.

here's the instantiation:
<cfset foo = createObject('component', 'CFCs.myCFC').init(datasource="#application.data#") />

now in the CFC itself, you have an init() method at the top:

<cfcomponent>
<cffunction name="init" returntype="myCFC" output="false">
<cfargument name="datasource" type="string" required="true" />
<cfset variables.data = arguments.datasource />
<cfreturn this />
</cfcomponent>

now, in all methods in that CFC, the variable #data# will be available, and will be whatever you initially passed into the init() method.

PS - are you sure the application scope is "hunted" by CF on non-scoped variables? I'm still pretty sure it's not 🙂
see -> http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000914.htm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

romeogq wrote:
> Am I able to read application variables from with in a CFC? If so, how?

do you have application.cfm or cfc in the dir w/your CFCs? is it pointing at the
same application name, application vars turned on, etc.?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

[CJ] wrote:
> in theory you can, but you shouldn't. CFCs should be as unaware of the world
> outside of themselves as possible.

except maybe facade CFCs which usually break at least one common-sense rule.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

quote:

Originally posted by: Newsgroup User
[CJ] wrote:
> in theory you can, but you shouldn't. CFCs should be as unaware of the world
> outside of themselves as possible.

except maybe facade CFCs which usually break at least one common-sense rule.



I'm still n00bish with CFCs in general, and haven't even broached the subject of facades. But I'll grant you that there are certainly exceptions to every rule, and I probably should have said, "in general..." or some such 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

CJ,
I'm not completely sure about the application scope... but for every server I've used for hosting so far, the application scope wasn't required.

Also, thanks for the dns suggestion. I will edit my cfcs to your first route. Question though... can second route be used f I am using the cfobject, cfc.function() method? I know it can be used in the first, just that it'll look cleaner to me without the dsn argument

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

<cfobject name="foo" component="CFCs.myCFC">
<cfinvoke
component="#foo#"
method="init"
datasource="#application.data#"
>

i think that should work. i've never used the cfobject or cfinvoke.

the method chaining syntax is preferred by some (and like everything else, shunned by others) because it prevents "race conditions". the CFC's init() method is called as the CFC is being instantiated. if it's 2 separate calls (one to create the object and then one to call the init() method), there's a chance that somebody could make a reference to it after its been created but before the init() method is called. this is really only an issue if you're saving the CFC into the application scope...but an issue to be aware of.

ps - i'm really curious about the scoped variables thing :) are you explicitly setting them into the application scope (e.g. <cfset application.data = "myDatasource" /> or are you setting them into the local scope on the Application.cfm page (e.g. <cfset data = "myDatasource" />)? i'm guessing it's the latter, which isn't setting the variable into the application scope...but setting it into the local (variables) scope on each page call. that would explain the fact that you've been able to reference them without scoping them (because they don't actually exist in the application scope).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 18, 2006 Dec 18, 2006

Copy link to clipboard

Copied

LATEST
quote:

Originally posted by: romeogq
Am I able to read application variables from with in a CFC? If so, how?

Code Snip:
<cfcomponent>
<cffunction name="function1" access="public" returntype="numeric">
<cfquery datasource="#data#" name="membersearch" maxrows="1">

Error:
Element DATA is undefined in APPLICATION.

If you have a datasource named data, take away the octothorps. If data is a variable, where do you set it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation