Skip to main content
May 30, 2010
Question

calling a cfc through a udf

  • May 30, 2010
  • 4 replies
  • 1249 views

Is it possible to call a cfc function through a udf, or maybe im just looking at this the wrong way but here is what im ultimately trying to do.

I have the following table:

tbl_users

=====================

uid                         name

=====================

1                         John

2                         Steve

3                         Joe

4                         Bob

Then throughout the program i am given a few of the uid, and i have to loop through them and get the names, so im trying to figure out a way to make a function so i can call it like this and it checks the db and gives me the name:

getName(uid) = returns name

Is there any easy way of doing something like this?

    This topic has been closed for replies.

    4 replies

    BKBK
    Community Expert
    Community Expert
    May 31, 2010

    case 1: user-defined function alone

    <cffunction name="getNameFromId">
        <cfargument name="id" default="1">
        <cfset var selectedUser = "">
        <cfset user = arrayNew(1)>
        <cfset user[1] = "John">
        <cfset user[2] = "Steve">
        <cfset user[3] = "Joe">
        <cfset user[4] = "Bob">
        <cfset selectedUser = user[arguments.id]>
        <cfreturn selectedUser>
    </cffunction>

    <cfoutput>#getNameFromId(3)#</cfoutput>

    case 2: (a) content of CFM page userName.cfm

    <cfset userObject = createobject("component", "User")>
    <cfoutput>#userObject.getNameFromId(2)#</cfoutput>


    <!--- Alternative, using cfinvoke --->
    <!---
    <cfinvoke component="User" method="getNameFromId" returnVariable="userName">
        <cfinvokeargument name="id" value="2">
    </cfinvoke>
    <cfoutput>#userName#</cfoutput>
    --->

    (b) The user-defined function is in a component saved as User.cfc in the same directory as the page userName.cfm

    <cfcomponent displayname="User">
    <cffunction name="getNameFromId">
        <cfargument name="id" default="1">
        <cfset var selectedUser = "">
        <cfset user = arrayNew(1)>
        <cfset user[1] = "John">
        <cfset user[2] = "Steve">
        <cfset user[3] = "Joe">
        <cfset user[4] = "Bob">
        <cfset selectedUser = user[arguments.id]>
        <cfreturn selectedUser>
    </cffunction>
    </cfcomponent>

    May 31, 2010

    Thank you for the response, but is there a sample that i can look at that would show me how to call a cfc through a udf? I've been searching around but haven't been able to find one.

    ilssac
    Inspiring
    May 31, 2010

    Do you know how to use and call a CFC?

    May 31, 2010

    Yeah, i use the cfinvoke, but i want to be able to use the function like this if possible:

    function(param)

    and then have that call the cfc, instead of having to type out the whole cfinvoke thing everytime i want to use it.

    Inspiring
    May 30, 2010

    The most efficient way of doing it is to run one query that gets all the names for you list of uids.

    ilssac
    Inspiring
    May 30, 2010

    If you data is as simple as an ID and a Name, a simple structure or array would surfice.

    I.E.

    <cfset myStruct = {

         1='John',
         2='Steve',
         3='Joe',
         4='Bob'
    }>

    <cfoutput>#myStruct[3]#</cfoutput>

    But, if your data is more complex.  Yes you can access a CFC from within a UDF just like any place else in your CFML.