Solved

Call function from other component.

Posted on 2012-03-27
5
408 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 150 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 350 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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