Skip to main content
Inspiring
April 17, 2008
Question

First CFC question

  • April 17, 2008
  • 8 replies
  • 543 views
Ok, I'm working on my first CFC, and having some problems. The code I have
so far is:

<cfcomponent displayname="duplicate" hint="Duplicates local club
tournaments">
<!--- This function retrieves all customers from the database --->
<cffunction name="getinfo"
hint="Gets all tournament info from the database">
<cfquery name="DuplicateTournamentList" datasource="SalleBoise">
select * from clubtournaments
where TournID=#session.tid#
</cfquery>

<cfset NewEventName=DuplicateTournamentList.TournName>
<cfset NewEventDesc=DuplicateTournamentList.TournDesc>
<cfset NewDateTime=dateadd("m",2,dateformat(TournDateTime,"mm/dd/yyyy"))>
<cfset NewTournFoil=DuplicateTournamentList.TournFoilEvent>
<cfset NewTournEpee=DuplicateTournamentList.TournEpeeEvent>
<cfset NewType=DuplicateTournamentList.EventType>
<cfset NewDuplicate=DuplicateTournamentList.TournDuplicated>

<cfquery name="NewTourn" datasource="SalleBoise">
insert into clubtournaments
(TournName,TournDesc,TournFoilEvent,TournEpeeEvent,EventType,TournDateTime,TournDuplicated)
values
(#NewEventName#,#NewEventDesc#,#NewTournFoil#,#NewTournEpee#,#NewType#,#NewDateTime#,#NewDuplicate#)
</cfquery>

<cfquery name="UpdateOldTourn" datasource="SalleBoise">
update clubtournaments
set TournDuplicated=1
where TournID=#session.tid#
</cfquery>

</cffunction>
</cfcomponent>

And I'm calling it with:

<cfinvoke component="duplicate.cfc" method="getinfo">

And I'm getting:

Error Occurred While Processing Request
Could not find the ColdFusion Component or Interface duplicate.cfc.
Ensure that the name is correct and that the component or interface exists.


    This topic has been closed for replies.

    8 replies

    Inspiring
    April 18, 2008
    I will give that a shot..... is there a way to find out if there is a error
    somewhere in the process.... or is being run at all??

    "Azadi" <azadi@sabai-dee.com> wrote in message
    news:fu95g0$t4$1@forums.macromedia.com...
    > oops, yes, correct - i must have missed that you were adding months to
    > the date returned by your first query.
    >
    > easily solved:
    >
    > note: syntax, again, is db-specific. example below is for mysql. your db
    > might have a different date function and syntax to use.
    >
    > also, assumes that TournDateTime column is of DATE or DATETIME or
    > TIMESTAMP datatype, NOT text or varchar
    >
    > <cfquery name="DuplicateTournamentList" datasource="SalleBoise">
    > INSERT INTO clubtournaments
    > (TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    > TournDateTime, TournDuplicated)
    > SELECT
    > TournName,
    > TournDesc,
    > TournFoilEvent,
    > TournEpeeEvent,
    > EventType,
    > DATE_ADD(TournDateTime INTERVAL 2 MONTH) AS TournDateTime,
    > 1 AS TournDuplicated
    > FROM clubtournaments
    > WHERE TournID = <cfqueryparam cfsqltype="cf_sql_integer"
    > value="#arguments.tid#">
    > </cfquery>
    >
    > in MS Access, i believe, the function is DateAdd(period, increment,
    > date), i.e. it will be DATEADD('m', 2, TournDateTime) AS TournDateTime
    >
    > hth
    >
    > Azadi Saryev
    > Sabai-dee.com
    > http://www.sabai-dee.com/


    Gene_Godsey
    Inspiring
    April 18, 2008
    You need the correct dot delimited path in the invokation from the CFM template.
    Your componet="address" is wrong. Find the correct path.
    Inspiring
    April 18, 2008
    hey, 12Robots, thanks for repeating my comments almost word-for-word.
    good job. :)

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    12Robots
    Participating Frequently
    April 21, 2008
    Azadi,

    I did not see your comments before I submitted mine. I am going to assume that your comment were tongue-in-cheek and not an accusation of some kind of plagiarism.

    12Robots
    Participating Frequently
    April 18, 2008
    Ok, steve. A few things you need to do.

    When you invoke you need to make sure of two thing.

    1. That you include the full path to the cfc using dot notation. If the cfc is in the same directory as the cfm page that is calling it, then you are fine (after you do the second part). If the cfc is not in the same directory, you need to specify the path. For examle, if the cfc is in a folder on your web root called "cfc" then your invoke would look like:

    <cfinvoke component="cfc.duplicate" method="getInfo" />

    2. You may have noticed that I left off the .cfc in the cfinvoke. That's the second part. Do not include .cfc when you are referencing a component. If the cfc is in the same directory as the calling cfm, then that is probably your issue.

    There are a couple other issues with your CFC that I will bring up, because they may cause you issues in the future. You probably will cross these bridges when you come to them, but I will point them out anyway

    1. You should var scope all of the variables in your function. This means you should scope them so that they are only available within the function, and then they die when the function completes. This means you will need to pass data out of your function before it completes using <cfreturn />

    To var scope a variable, move them all to the top of the function and set them as:

    <cfset VAR myvar="Default" />

    You should also var scope your queries by scoping their names with empty values/

    <cfset VAR qMyQuery = "" />

    All var scoping needs to be done at the top of the function.

    2. Accessing the session scope from your component is, most of the time, considered bad form because it violates encapsulation. Instead of referencing session.tid directly from your cfc, you should pass it in during your cf invoke and then receive it in your cfc.

    Your invoke would look like:
    <cfinvoke component="cfc.duplicate" method="getInfo" tid="session.tid" />

    And at the top of your function, even above your var scoping you would enter:

    <cfargument name="tid" type="numeric" />

    Then, anywhere that you needed that variable you would reference it as arguments.tid.

    So your first query would be redone as:

    <cfquery name="DuplicateTournamentList" datasource="SalleBoise">
    select * from clubtournaments
    Where TournID=#arguments.tid#
    </cfquery>


    There are several other things you could do with your CFC that could improve performance and readability. However, if it is your first attempt, the bully! Well done.
    Inspiring
    April 18, 2008
    oops, yes, correct - i must have missed that you were adding months to
    the date returned by your first query.

    easily solved:

    note: syntax, again, is db-specific. example below is for mysql. your db
    might have a different date function and syntax to use.

    also, assumes that TournDateTime column is of DATE or DATETIME or
    TIMESTAMP datatype, NOT text or varchar

    <cfquery name="DuplicateTournamentList" datasource="SalleBoise">
    INSERT INTO clubtournaments
    (TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    TournDateTime, TournDuplicated)
    SELECT
    TournName,
    TournDesc,
    TournFoilEvent,
    TournEpeeEvent,
    EventType,
    DATE_ADD(TournDateTime INTERVAL 2 MONTH) AS TournDateTime,
    1 AS TournDuplicated
    FROM clubtournaments
    WHERE TournID = <cfqueryparam cfsqltype="cf_sql_integer"
    value="#arguments.tid#">
    </cfquery>

    in MS Access, i believe, the function is DateAdd(period, increment,
    date), i.e. it will be DATEADD('m', 2, TournDateTime) AS TournDateTime

    hth

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    April 18, 2008
    The 1 question I have with your code that is different than mine is the
    query. Mine, I had hoped would take the info from the previous event,
    and add a number of months to it before adding it into the table.

    It looks like yours simple duplicates the event without changing that
    date, correct?

    Azadi wrote:
    > first, rtfm about <cfinvoke> tag. the component attribute needs a
    > dot-delimited path to your cfc, i.e. if your cfc is stored in a
    > components folder under web root and your calling template is also in
    > the webroot: component="components.duplicate"
    >
    > then, it is not a good practice to access outside variables from within
    > the cfc - in your case you are accessing session vars. you better pass
    > them in to your cfc as arguments when you invoke it and have
    > <cfargument> tags in your function that accept them (see code at the bottom)
    >
    > <cfinvoke component="components.duplicate" method="getinfo"
    > tid="#session.tid">
    >
    > or
    >
    > <cfinvoke component="components.duplicate" method="getinfo">
    > <cfinvokeargument name="tid" value="#session.tid#">
    > </cfinvoke>
    >
    > then, variable scoping - always scope any cfc vars with var: <cfset var
    > somevar = something>. this will help you avoid variables confusion when
    > your calling page has vars with same names.
    >
    > then, even inside cfc, you should always use <cfqueryparam> tags in your
    > queries.
    >
    > as for your queries - the 3 of them can be combined easily into one.
    >
    > so your function in the end can look something like:
    > [note: query syntax is db-specific; check your db for correct syntax to use]
    >
    > <cffunction name="getinfo"
    > hint="Gets all tournament info from the database" returntype="boolean"
    > output="no">
    > <cfargument name="tid" required="yes" type="numeric">
    > <cfset var DuplicateTournamentList = "">
    > <cfset var result = true>
    > <cftry>
    > <cfquery name="DuplicateTournamentList" datasource="SalleBoise">
    > INSERT INTO clubtournaments
    > (TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    > TournDateTime, TournDuplicated)
    > SELECT TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    > TournDateTime, 1 AS TournDuplicated
    > FROM clubtournaments
    > WHERE TournID = <cfqueryparam cfsqltype="cf_sql_integer"
    > value="#arguments.tid#">
    > </cfquery>
    > <cfcatch type="any">
    > <cfset result = false>
    > </cfcatch>
    > </cftry>
    > <cfreturn result />
    > </cffunction>
    >
    > and your cfinvoke something like the examples above.
    >
    > hth
    >
    >
    > Azadi Saryev
    > Sabai-dee.com
    > http://www.sabai-dee.com/
    Inspiring
    April 18, 2008
    Wow, sounds like I really don't know much..... but I appreciate all the help

    Azadi wrote:
    > first, rtfm about <cfinvoke> tag. the component attribute needs a
    > dot-delimited path to your cfc, i.e. if your cfc is stored in a
    > components folder under web root and your calling template is also in
    > the webroot: component="components.duplicate"
    >
    > then, it is not a good practice to access outside variables from within
    > the cfc - in your case you are accessing session vars. you better pass
    > them in to your cfc as arguments when you invoke it and have
    > <cfargument> tags in your function that accept them (see code at the bottom)
    >
    > <cfinvoke component="components.duplicate" method="getinfo"
    > tid="#session.tid">
    >
    > or
    >
    > <cfinvoke component="components.duplicate" method="getinfo">
    > <cfinvokeargument name="tid" value="#session.tid#">
    > </cfinvoke>
    >
    > then, variable scoping - always scope any cfc vars with var: <cfset var
    > somevar = something>. this will help you avoid variables confusion when
    > your calling page has vars with same names.
    >
    > then, even inside cfc, you should always use <cfqueryparam> tags in your
    > queries.
    >
    > as for your queries - the 3 of them can be combined easily into one.
    >
    > so your function in the end can look something like:
    > [note: query syntax is db-specific; check your db for correct syntax to use]
    >
    > <cffunction name="getinfo"
    > hint="Gets all tournament info from the database" returntype="boolean"
    > output="no">
    > <cfargument name="tid" required="yes" type="numeric">
    > <cfset var DuplicateTournamentList = "">
    > <cfset var result = true>
    > <cftry>
    > <cfquery name="DuplicateTournamentList" datasource="SalleBoise">
    > INSERT INTO clubtournaments
    > (TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    > TournDateTime, TournDuplicated)
    > SELECT TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    > TournDateTime, 1 AS TournDuplicated
    > FROM clubtournaments
    > WHERE TournID = <cfqueryparam cfsqltype="cf_sql_integer"
    > value="#arguments.tid#">
    > </cfquery>
    > <cfcatch type="any">
    > <cfset result = false>
    > </cfcatch>
    > </cftry>
    > <cfreturn result />
    > </cffunction>
    >
    > and your cfinvoke something like the examples above.
    >
    > hth
    >
    >
    > Azadi Saryev
    > Sabai-dee.com
    > http://www.sabai-dee.com/
    Inspiring
    April 18, 2008
    first, rtfm about <cfinvoke> tag. the component attribute needs a
    dot-delimited path to your cfc, i.e. if your cfc is stored in a
    components folder under web root and your calling template is also in
    the webroot: component="components.duplicate"

    then, it is not a good practice to access outside variables from within
    the cfc - in your case you are accessing session vars. you better pass
    them in to your cfc as arguments when you invoke it and have
    <cfargument> tags in your function that accept them (see code at the bottom)

    <cfinvoke component="components.duplicate" method="getinfo"
    tid="#session.tid">

    or

    <cfinvoke component="components.duplicate" method="getinfo">
    <cfinvokeargument name="tid" value="#session.tid#">
    </cfinvoke>

    then, variable scoping - always scope any cfc vars with var: <cfset var
    somevar = something>. this will help you avoid variables confusion when
    your calling page has vars with same names.

    then, even inside cfc, you should always use <cfqueryparam> tags in your
    queries.

    as for your queries - the 3 of them can be combined easily into one.

    so your function in the end can look something like:
    [note: query syntax is db-specific; check your db for correct syntax to use]

    <cffunction name="getinfo"
    hint="Gets all tournament info from the database" returntype="boolean"
    output="no">
    <cfargument name="tid" required="yes" type="numeric">
    <cfset var DuplicateTournamentList = "">
    <cfset var result = true>
    <cftry>
    <cfquery name="DuplicateTournamentList" datasource="SalleBoise">
    INSERT INTO clubtournaments
    (TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    TournDateTime, TournDuplicated)
    SELECT TournName, TournDesc, TournFoilEvent, TournEpeeEvent, EventType,
    TournDateTime, 1 AS TournDuplicated
    FROM clubtournaments
    WHERE TournID = <cfqueryparam cfsqltype="cf_sql_integer"
    value="#arguments.tid#">
    </cfquery>
    <cfcatch type="any">
    <cfset result = false>
    </cfcatch>
    </cftry>
    <cfreturn result />
    </cffunction>

    and your cfinvoke something like the examples above.

    hth


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/