Solved

Call function from other component.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function 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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 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

23 Experts available now in Live!

Get 1:1 Help Now