Solved

Call function from other component.

Posted on 2012-03-27
5
403 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 15

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now