Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

How To Share Query Data Between Two ColdFusion CFCs?

Hi,

I am creating a CFC called "core_appdata". This CFC will hold the core stored procedures for an application.  Example....

<cfcomponent displayname="core_appdata" hint="I Return Core App Data" output="no">

<cffunction name="getprogram_list">	

      <cfargument name="getstoredproc_input_campaignid" type="string" required="false">
       <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
        <cfargument name="getstoredproc_input_startdate" type="date" required="false">
        <cfargument name="getstoredproc_input_enddate" type="date" required="false">
        <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
        <cfargument name="getstoredproc_input_listid" type="string" required="false">
        <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
		
		<cfset var rst_getprogram_list ="">
		 ---  stored proc --- 			 
			 <cfstoredproc procedure  = "p_adb_getprogram_list">
			 </cfstoredproc>
		 
		<cfreturn rst_getprogram_list />
	</cffunction>
	
</cfcomponent>  

Open in new window


I would also like to create a CFC called "core_appdata_grids". This CFC would be used to bind to cfgrids and allow paging etc. In a perfect world, this CFC would get its data from the method/function "getprogram_list" in the CFC "core_appdata" above. Example...


<cfcomponent displayname="core_appdata_grids" hint="I Return Core App Data For CFGrids " output="no">

	<cffunction name="getprogram_list_grid">
	
               <cfargument name="page" required="no" />
		<cfargument name="pageSize" required="no" />
		<cfargument name="gridsortcolumn" required="no" />
		<cfargument name="gridsortdirection" required="no" />	
		<cfargument name="getstoredproc_input_campaignid" type="string" required="false">
               <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
               <cfargument name="getstoredproc_input_startdate" type="date" required="false">
               <cfargument name="getstoredproc_input_enddate" type="date" required="false">
               <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
               <cfargument name="getstoredproc_input_listid" type="string" required="false">
                <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
		
		<cfset var rst_getprogram_list_grid ="">
		 ---  get data --- 			 
		
		
		<cfreturn queryconvertforgrid(rst_getprogram_list_grid, page, pagesize) />
	</cffunction>
	
</cfcomponent>  

Open in new window



Questions:
- Is this possible? If so, how is it done?
- If so is this best practice when working with CFCs?
- If not, what is the best way to share data between CFCs


Thank you in advance for your time in helping me with this question.

DD
0
datadirector
Asked:
datadirector
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
srikanthmadishettiCommented:
Create a object for core_appdata in core_appdata_grids and use that function or  make core_appdata super cfc for core_appdata_grids using extend attribute  of cfcomponent .
0
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
Now you have two Components

1. core_appdata
2. core_appdata_grids

There are two ways you can do,

1. use the extends atribute of the seconf component and pass the name of the first component like this
<cfcomponent displayname="core_appdata_grids" hint="I Return Core App Data For CFGrids " output="no" extends="core_appdata">

This way all your methods in the Core_appdata will be avaliable to you calling CFC, in this case it is core_appdate_grids

inside the function u can just use like this

<cffunction name="getprogram_list_grid">
	
               <cfargument name="page" required="no" />
		<cfargument name="pageSize" required="no" />
		<cfargument name="gridsortcolumn" required="no" />
		<cfargument name="gridsortdirection" required="no" />	
		<cfargument name="getstoredproc_input_campaignid" type="string" required="false">
               <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
               <cfargument name="getstoredproc_input_startdate" type="date" required="false">
               <cfargument name="getstoredproc_input_enddate" type="date" required="false">
               <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
               <cfargument name="getstoredproc_input_listid" type="string" required="false">
                <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
		
		<cfset var rst_getprogram_list_grid ="getprogram_list()">

		
		
		<cfreturn queryconvertforgrid(rst_getprogram_list_grid, page, pagesize) />
	</cffunction>

Open in new window


I am calling the base file's function inside your grid cfc

2. You ca actually use the cfobject tag inside the component to call the cfc also like this

You second CFC which is

<cfcomponent displayname="core_appdata_grids" hint="I Return Core App Data For CFGrids " output="no">
<cfset newCFC = CreateObject("component","core_appdate")>
	<cffunction name="getprogram_list_grid">
	
               <cfargument name="page" required="no" />
		<cfargument name="pageSize" required="no" />
		<cfargument name="gridsortcolumn" required="no" />
		<cfargument name="gridsortdirection" required="no" />	
		<cfargument name="getstoredproc_input_campaignid" type="string" required="false">
               <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
               <cfargument name="getstoredproc_input_startdate" type="date" required="false">
               <cfargument name="getstoredproc_input_enddate" type="date" required="false">
               <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
               <cfargument name="getstoredproc_input_listid" type="string" required="false">
                <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
		
		<cfset var rst_getprogram_list_grid ="newCFC.getprogram_list()">
		 ---  get data --- 			 
		
		
		<cfreturn queryconvertforgrid(rst_getprogram_list_grid, page, pagesize) />
	</cffunction>
	
</cfcomponent>  

Open in new window


That is how it will be done

Cheers
0
 
srikanthmadishettiCommented:
@myselfrandhawa  -

The following statements are wrong
>> <cfset var rst_getprogram_list_grid ="getprogram_list()">
>> <cfset var rst_getprogram_list_grid ="newCFC.getprogram_list()">

That will take it as string  and the variable  rst_getprogram_list_grid will have value newCFC.getprogram_list() stored as string . you don't need that double quotes.

and I see that you are trying to give the same solution which I gave already  :)  .
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
datadirectorAuthor Commented:
@myselfrandhawa

Thank you for your explanation and time, it makes perfect sense. At this time I am also reading Matt Gifford's "Book Object-Oriented Programming In ColdFusion" to help me get a better grip of some of these OOP/CFC concepts.. but with that said I have an additional question.

Does the child CFC "core_appdata_grids" have to pass arguments into the extended/parent CFC "core_appdata" as well to access the data from the parent CFC?

I do not want the child CFC "core_appdata_grids" to have the ability to modify the returned data from the parent CFC via arguments. Rather, I would like the child CFC to simply have access to that data returned by the parent CFC which executes first.

At that point all the CFC "core_appdata_grids" does is bind to a CFGRID and would have only the arguments needed for CFGRID paging etc:

<cfcomponent displayname="core_appdata_grids" hint="I Return Core App Data For CFGrids " output="no" extends="core_appdata">

	<cffunction name="getprogram_list_grid">
	
               <cfargument name="page" required="no" />
		<cfargument name="pageSize" required="no" />
		<cfargument name="gridsortcolumn" required="no" />
		<cfargument name="gridsortdirection" required="no" />	
	
		<cfset var rst_getprogram_list_grid ="getprogram_list()">
		
		
		<cfreturn queryconvertforgrid(rst_getprogram_list_grid, page, pagesize) />
	</cffunction>

Open in new window



Is this doable when extending a CFC or recommended practice Or would you simply modify the parent CFC "core_appdata" to return two recordsets:

1.) As a Query
2.) Query Converted For Grids?


Thanks in advance for your continued help.

DD
0
 
srikanthmadishettiCommented:
DD

CHILD cfc can use the functions of the parent CFC

So in core_appdata_grids you can call getprogram_list_grid


just use this

<cfset getprogram_list_grid =Super..getprogram_list(pass all the variables needed)>


so your core_appdata_grids  code will be like


<cfcomponent displayname="core_appdata_grids" hint="I Return Core App Data For CFGrids " output="no" extend = "core_appdata">

      <cffunction name="getprogram_list_grid">
      
               <cfargument name="page" required="no" />
            <cfargument name="pageSize" required="no" />
            <cfargument name="gridsortcolumn" required="no" />
            <cfargument name="gridsortdirection" required="no" />      
            <cfargument name="getstoredproc_input_campaignid" type="string" required="false">
               <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
               <cfargument name="getstoredproc_input_startdate" type="date" required="false">
               <cfargument name="getstoredproc_input_enddate" type="date" required="false">
               <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
               <cfargument name="getstoredproc_input_listid" type="string" required="false">
                <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
            
            <cfset var rst_getprogram_list_grid =Super..getprogram_list(pass all the variables needed))>
             ---  get data ---                   
            
            
            <cfreturn queryconvertforgrid(rst_getprogram_list_grid, page, pagesize) />
      </cffunction>
      
</cfcomponent>
0
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
@sri: actually Super is not required in the case, The way i have told him to actually extends and or evn call the cfc will work and calling the function and storing the function in a var will work

<cfset var ck = getActivata()>

<cfif ck.emailID NEQ "">
Process Works
<cfelse>
Not work
</cfif>

I have done this many times and it works, i do not know how u are saying it is wrong
0
 
srikanthmadishettiCommented:
@ myselfrandhawa

 LOL you misunderstood my point , Pls look at  my first comment

, you  mentioned ur code like this

<cfset var rst_getprogram_list_grid ="getprogram_list()">

this what i was referring to as wrong

Yes you are correct  don't need super key , i just mentioned it to show if needed we can use same function name - This technique lets your subclassed component override a method without losing the ability to call the original version of the method.


what I was suggesting like below

core_appdata.cfc
<cfcomponent displayname="core_appdata" hint="I Return Core App Data" output="no">

<cffunction name="getprogram_list">	

      <cfargument name="getstoredproc_input_campaignid" type="string" required="false">
       <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
        <cfargument name="getstoredproc_input_startdate" type="date" required="false">
        <cfargument name="getstoredproc_input_enddate" type="date" required="false">
        <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
        <cfargument name="getstoredproc_input_listid" type="string" required="false">
        <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
		
		<cfset var rst_getprogram_list ="">
		 ---  stored proc --- 			 
			 <cfstoredproc procedure  = "p_adb_getprogram_list">
			 </cfstoredproc>
		 
		<cfreturn rst_getprogram_list />
	</cffunction>
	
</cfcomponent>  

Open in new window




core_appdata_grids

<cfcomponent displayname="core_appdata_grids" hint="I Return Core App Data For CFGrids " output="no">

	<cffunction name="getprogram_list">
	
               <cfargument name="page" required="no" />
		<cfargument name="pageSize" required="no" />
		<cfargument name="gridsortcolumn" required="no" />
		<cfargument name="gridsortdirection" required="no" />	
		<cfargument name="getstoredproc_input_campaignid" type="string" required="false">
               <cfargument name="getstoredproc_input_filtertestrecs" type="string" required="false">
               <cfargument name="getstoredproc_input_startdate" type="date" required="false">
               <cfargument name="getstoredproc_input_enddate" type="date" required="false">
               <cfargument name="getstoredproc_input_listtypeid" type="string" required="false">
               <cfargument name="getstoredproc_input_listid" type="string" required="false">
                <cfargument name="getstoredproc_input_appenvr" type="string" required="false">
		
				
		<cfreturn queryconvertforgrid(super.getprogram_list(pass needed aruguemnts), page, pagesize) />
	</cffunction>
	
</cfcomponent>  

Open in new window

0
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
i agree, i seen later, i wrapped the quotes which was rather not required,

:)
0
 
_agx_Commented:
<cfset var rst_getprogram_list_grid =Super..getprogram_list(pass all the variables needed))>

Small tip, if both functions share the same argument names, can use the short-cut argumentCollection to pass the arguments from one function to another:

    <cfset var theResult = parentFunctionName( argumentCollection=arguments )>


               <cfargument name="page" required="no" />
            <cfargument name="pageSize" required="no" />
                ...

                                 
Since you're always using those arguments within the function they should be required="yes". Otherwise, an error will occur if it's omitted.  And the variables should be scoped ie <cfreturn queryconvertforgrid(..., arguments.page, ....)>

       
      <cfargument name="getstoredproc_input_campaignid" ...>

You might also consider shortening some of the variable names. They should be descriptive. But the fact that the results come from a stored procedure isn't relevant. So prefacing them all with "getstoredproc_input_" just tends to decrease readability. I would recommend shorter (but still descriptive) names like this:

            <cfargument name="campaignID" ...>
               <cfargument name="startDate" ...>
               <cfargument name="endDate" ...>
0
 
srikanthmadishettiCommented:
Nice tip agx
0
 
datadirectorAuthor Commented:
@myselfrandhawa, @srikanthmadishetti

Thank you very much for your help.  Your solutions have helped me move forward in the right direction as I work towards re-writing my app using CFCs.

Thanks again,

DD
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now