?
Solved

Call function from other component.

Posted on 2012-03-27
5
Medium Priority
?
410 Views
Last Modified: 2012-03-27
Hello experts.
I have in my application file two createobject:
<cfset objcategories = CreateObject("component", "admin.com.categories").init(dsn = "#request.dsn#")>
<cfset objhtaccess = CreateObject("component", "admin.com.createhtaccess").init(dsn = "#request.dsn#")>

The first one is handling all the categories stuff and the second is creating and updating the htaccess file.
Now if i add or update my categories i want to recreate this htaccess file.

f.e.
delete categorie:
<cfset qrydeletecategory = objcategories.fdeletecategory(url.categoryid)>
cffunction:
<cffunction name="fdeletecategory" access="public" returntype="boolean">
      <cfargument name="categoryid" type="numeric" required="yes">
        <cfquery name = "DeleteCategory" datasource="#variables.dsn#">
        DELETE FROM categories
        WHERE CategoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.categoryid#">
        </cfquery>
       <!--- CREATE HTACCESS AND SITEMAP ? --->
        <cfreturn true>
    </cffunction>
For creating the htaccess file:
<cfset qryhtaccess = objhtaccess.writehtaccess(request.rewriteruleformat)>

The question is.
Can i call the writehtaccess function inside the fdeletecategory function?
Is this the right way?
0
Comment
Question by:Panos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 16

Assisted Solution

by:Gurpreet Singh Randhawa
Gurpreet Singh Randhawa earned 600 total points
ID: 37771028
yes you can do that, and you can do it this way:

<cfset objhtaccess = CreateObject("component", "admin.com.createhtaccess").init(dsn = "#request.dsn#")>

<cffunction name="fdeletecategory" access="public" returntype="boolean">
      <cfset var qryhtaccess = objhtaccess.writehtaccess(request.rewriteruleformat)>
      <cfargument name="categoryid" type="numeric" required="yes">
        <cfquery name = "DeleteCategory" datasource="#variables.dsn#">
        DELETE FROM categories
        WHERE CategoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.categoryid#">
        </cfquery>
       <!--- CREATE HTACCESS AND SITEMAP ? --->
        <cfreturn true>
    </cffunction>

now suppose you have a delete cfc and cfcomponent start and end and inside that you are doing all your functions, in the above case i showed, i have called another component of htaaccess, in a separate line.

you have two options here

1. use the code above as mine

2. if you do not want to create the objhtaaceess as i did, just use the extends attribute of cfcompoent tag and point it to the htaaccess cfc, this way it will inherit all the public methods of objhtaccess to your delete cfc and you ca use them anywhere like i did, but use inside the cffunction

Cheers
0
 
LVL 2

Author Comment

by:Panos
ID: 37771286
Hi myselfrandhawa.
case 1.
I'm creating the objhtaccess in the application file. Do i have to create it again in the delete cfc ?
case 2.
...just use the extends attribute of cfcompoent tag and point it to the htaaccess cfc...
please explain this in more detail .
0
 
LVL 2

Author Comment

by:Panos
ID: 37771597
hmmm.
I made some tests and realized that i have to place the createobject in the cfc.

I need only a clarification for case 2.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 1400 total points
ID: 37771754
Can i call the writehtaccess function inside the fdeletecategory function?

Yes. If it's stateless, there are 3 choices
1) recreate the createhtaccess object every time you call the delete function
     ie Use cfinvoke component="admin.com.createhtaccess"

2) create a new instance inside init() and store it in the variables scope. That means you'll have only 2 instances instead of 1 each time you call the delete function

      <cffunction name="init" ..>
               <cfargument name="dsn" ...>
               <cfset variables.dsn = arguments.dsn>
              <cfset variables.htaccess = createObject("admin.com.createhtaccess").init(arguments.dsn)>
     </cffunction>

3) pass the instance created inside your application file into init() as an argument. Then store it in the variables scope and use that object when you call delete. So you'll only have 1 instance in the entire application

<!--- create htaccess first --->
<cfset objhtaccess = CreateObject("component", "admin.com.createhtaccess").init(dsn = request.dsn )>
<!--- pass it in as an argument --->
<cfset objcategories = CreateObject("component", "admin.com.categories").init(dsn = request.dsn, htaccess=objhtaccess)>

      <!--- init function of admin.com.categories --->
      <cffunction name="init" ..>
               <cfargument name="dsn" ...>
               <cfargument name="htaccess" ...>
               <cfset variables.dsn = arguments.dsn>
               <cfset variables.htaccess = arguments.htaccess>
      </cffunction>

       Use the variables scoped object inside your delete function

        <!--- CREATE HTACCESS AND SITEMAP ? --->
        <cfset variables.htaccess.callSomeFunctionHere(...)>

I need only a clarification for case 2.

Honestly I wouldn't use the extends hack unless the components are truly related, which they don't seem to be. If they're not, better to keep them separate and pass in the component using one of the methods above. Preferably #2 or #3
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 37772052
Thank you for your help.
regards
panos
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question