Solved

Call function from other component.

Posted on 2012-03-27
5
407 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
  • 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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