ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2011, 09:26 AM
rbmako69 rbmako69 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 33 rbmako69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 58 m 36 sec
Reputation Power: 7
Datasource variable not working in CFC

I'm creating a new feature using a cfc, which the current app doesn't utilize right now. I need to query our database, but the variable that I have set doesn't seem to be working, and just returning an empty string. Here is what I have to initialize the cfc.

Application.cfm
Code:
<cfinvoke 
 component="cfc.calcMPS"
 method="Init">
	<cfinvokeargument name="DataSource" value="#dsn#"/>
</cfinvoke>


calcMPS.cfc:
Code:
<cfcomponent displayname="mpsCostCalculation">
<cfset datasource = "">
		
    <cffunction name="Init">
		<cfargument required="Yes" name="DataSource" type="string">
            <cfset datasource = arguments.DataSource>
      </cffunction>

...


And this is what it says in the debugging information

Quote:
CFC[ C:\ColdFusion9\wwwroot\cfc\calcMPS.cfc | Init(DataSource = ve0_aaas) ] from C:\ColdFusion9\wwwroot\cfc\calcMPS.cfc

Reply With Quote
  #2  
Old August 11th, 2011, 11:20 AM
kiteless kiteless is offline
Moderator
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 5,100 kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 4 h 29 m 54 sec
Reputation Power: 966
I suspect it is because init() is reserved as the constructor method for a CFC. Try changing the method name to getDatsource() or something similar.

Reply With Quote
  #3  
Old August 11th, 2011, 12:18 PM
rbmako69 rbmako69 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 33 rbmako69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 58 m 36 sec
Reputation Power: 7
I'll give that a shot, but I thought that the init function was to initialize "setup" all variables used throughout the CFC? I've used CFC's in the past, but never from scratch, so I'm in unknown waters.

check out this link for a reference of what I'm trying to do, but doesn't seem to work. Am I missing something?

I don't know if it makes a difference, but we are still using CF 7

Reply With Quote
  #4  
Old August 11th, 2011, 01:02 PM
kiteless kiteless is offline
Moderator
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 5,100 kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 4 h 29 m 54 sec
Reputation Power: 966
Yes, it makes a big difference, since CF7 is over six years old.

Have you read the documentation on CFCs? Because you don't need to use cfinvoke for this. You can just do:

Code:
<cfset mpsCostCalculation = CreateObject( 'component', 'cfc.calcMPS' ) />
<cfset mpsCostCalculation.setDataSource( dsn ) />


Assuming you name your setter method "setDataSource".

But yes, CFCs and object-oriented programming are vast subjects so you really want to read the docs or get a book in order to understand it.

Reply With Quote
  #5  
Old August 11th, 2011, 01:24 PM
rbmako69 rbmako69 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 33 rbmako69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 58 m 36 sec
Reputation Power: 7
Thanks, that worked, but I'm still confused on why this wasn't working. If I called the init() method function from the application.cfm using:

Code:
<cfset datasource = CreateObject( 'component', 'cfc.calcMPS' ).init(dsn) />




Code:
<cfcomponent displayname="mpsCostCalculation">   
<cfset variables.dsn =  ''> <!--setting the global datasource --->

<cffunction name="init" access="public" returntype="any">
		<cfargument required="Yes" name="dsn" type="string">
        <cfset variables.dsn = #arguments.dsn#>
		<cfreturn this>
    </cffunction>


The component get instantiated by init, and sets the variables.dsn to null, then calls the init,and sets variables.dsn to the value that is passed into the init method.

Or am I just not thinking about it correctly, because it seemed as if the variables.dsn value was getting set back to null when I called the other method that was running the query in question.

Reply With Quote
  #6  
Old August 11th, 2011, 01:54 PM
kiteless kiteless is offline
Moderator
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 5,100 kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 4 h 29 m 54 sec
Reputation Power: 966
It's not working because the method isn't returning a value. If you want it to return something add

<cfreturn dataSource />

to the end of the cffunction contents.

Again, this is all explained in the docs.

Reply With Quote
  #7  
Old August 11th, 2011, 01:58 PM
kiteless kiteless is offline
Moderator
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 5,100 kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level)kiteless User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 4 h 29 m 54 sec
Reputation Power: 966
To add, the way this is typically done would be for the CFC to look like:

Code:
<cfcomponent displayname="mpsCostCalculation">
	
	<cfset datasource = "">
		
    <cffunction name="init">
		<cfargument required="Yes" name="DataSource" type="string">
		<cfset setDataSource( arguments.DataSource ) />
		<cfreturn this />
	</cffunction>
    
	<cffunction name="setDataSource">
		<cfargument required="Yes" name="DataSource" type="string">
		<cfset datasource = arguments.DataSource />
	</cffunction>
    
	<cffunction name="getDataSource">
		<cfreturn datasource />
	</cffunction>
	  
</cfcomponent>      


And then the calling code would be:

Code:
<cfset calcMPS = CreateObject( 'component', 'cfc.calcMPS' ).init( dsn ) />
<cfset datasource = calcMPS.getDataSource() />
Comments on this post
rbmako69 agrees: helpful, and did answer my initial question, but never really gave me an understanding of why I was
having the problem I was.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Datasource variable not working in CFC

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap