#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    33
    Rep Power
    8

    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

    CFC[ C:\ColdFusion9\wwwroot\cfc\calcMPS.cfc | Init(DataSource = ve0_aaas) ] from C:\ColdFusion9\wwwroot\cfc\calcMPS.cfc
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,242
    Rep Power
    967
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    33
    Rep Power
    8
    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
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,242
    Rep Power
    967
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    33
    Rep Power
    8
    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.
  10. #6
  11. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,242
    Rep Power
    967
    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.
  12. #7
  13. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,242
    Rep Power
    967
    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.

IMN logo majestic logo threadwatch logo seochat tools logo