Skip to main content
Known Participant
March 9, 2012
Question

CFC function multiple queries returns

  • March 9, 2012
  • 2 replies
  • 7174 views

I have a cfc and I am wanting to have a function return multiple queries?  Is that possible? Here is what I have .. and I cant seem to get it to work. Thanks.

<cffunction name="classvideo" access="public" returntype="query">

  <cfset var classvideo="">

  <cfif not IsDefined("URL.VideoID")>

    <cflocation url="class.cfm?videoID=486">

  <cfelse>

    <cfquery name="classvideo" datasource="dsn">

          SELECT video_path, ID, Video_Name

    FROM Video

    WHERE ID = <cfqueryparam value="#URL.VideoID#" cfsqltype="cf_sql_integer">

          </cfquery>

  </cfif>

 

  <cfquery name="VideoList" datasource="dsn">

          SELECT Video_Name, Video_Length, ID, category, School_Year

    FROM Video

    WHERE category = 'class'AND School_Year = '11-12'

</cfquery><!------>

  <cfreturn classvideo>

</cffunction>

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    March 11, 2012

    TheScarecrow wrote:

    I have a cfc and I am wanting to have a function return multiple queries?  Is that possible? Here is what I have .. and I cant seem to get it to work. Thanks.

    <cffunction name="classvideo" access="public" returntype="query">

      <cfset var classvideo="">

      <cfif not IsDefined("URL.VideoID")>

        <cflocation url="class.cfm?videoID=486">

      <cfelse>

        <cfquery name="classvideo" datasource="dsn">

              SELECT video_path, ID, Video_Name

        FROM Video

        WHERE ID = <cfqueryparam value="#URL.VideoID#" cfsqltype="cf_sql_integer">

              </cfquery>

      </cfif>

      <cfquery name="VideoList" datasource="dsn">

              SELECT Video_Name, Video_Length, ID, category, School_Year

        FROM Video

        WHERE category = 'class'AND School_Year = '11-12'

    </cfquery><!------>

      <cfreturn classvideo>

    </cffunction>

    You posted very similar code in a previous thread. You haven't used the most valuable suggestion people gave you there, namely, passing the video ID as an argument. The result is that you get people to waste their time here repeating advice already given to you elsewhere.

    Think about your CFC yourself. (For a start, you must use the <cfcomponent> tag). Its context is different from that of a CFM page. So what happens when the CFM page opens, bringing in a videoID via the URL?

    The page knows URL.videoID, but the CFC does not. So when you instantiate the CFC and call the function, URL.videoID will be undefined and the function will redirect you to class.cfm?videoID=486.

    Presumably, on the page class.cfm you will attempt to instantiate the CFC and call the function again. If this is indeed so, you will end up in an infinite loop. This would have manifested itself as server failure.

    As colleagues have said, pass videoID as an argument. You know it makes sense. Good luck.

    Owainnorth
    Inspiring
    March 9, 2012

    Yup, there are three ways I can initially think of.

    1) Set returntype="array", and return an array of queries

    better...

    2) Set returntype="struct", and return a struct with two properties which are the querysets, which you can name

    even better...

    3) Create a class which has two properties which are the query objects, you can then pass it around as a "strongly-typed" object. For example

    Create a cfc:

    <cfcomponent name="MyMethodReturnObject">

      <cfproperty name="Prop1" type="Query" />

      <cfproperty name="Prop2" type="Query" />

      <cffunction name="init" access="public" returntype="MyMethodReturnObject" >

        <cfargument name="P1" type="query" required="true" />

        <cfargument name="P2" type="query" required="true" />

        <cfset Prop1 = arguments.P1 />

        <cfset Prop2 = arguments.P2 />

      </cffunction>

    Then in your calling CFC, run the two queries, then do this:

    <cfreturn new MyMethodReturnObject(q1, q2) />

    Job done, pow pow pow.

    </cfcomponent>

    Known Participant
    March 9, 2012

    So something like this? how do I access them on the page that is calling it?

    <cffunction name="classvideo" access="public" returntype="query">

         <cfset var videoStruct=StructNew()>

      <cfif not IsDefined("URL.VideoID")>

        <cflocation url="class.cfm?videoID=486">

      <cfelse>

        <cfquery name="classvideo" datasource="dsn">

              SELECT video_path, ID, Video_Name

        FROM Video

        WHERE ID = <cfqueryparam value="#URL.VideoID#" cfsqltype="cf_sql_integer">

              </cfquery>

      </cfif>

      <cfquery name="VideoList" datasource="dsn">

              SELECT Video_Name, Video_Length, ID, category, School_Year

        FROM Video

        WHERE category = 'class'AND School_Year = '11-12'

    </cfquery><!------>

    <cfset videoStruct.aged240video = aged240video>

      <cfset videoStruct.VideoList= VideoList>

      <cfreturn videoStruct>

    </cffunction>

    Inspiring
    March 9, 2012

    Not quite.

    <cffunction name="classvideo" access="public" returntype="query">

    The function is returning a structure, not a query.

      <cfif not IsDefined("URL.VideoID")>

    The function should define VideoID as a required argument, then use #arguments.VideoID# instead of #URL.VideoID#.

            ie   <cffunction ....>

                     <cfargument name="VideoID" .....>

    <cfquery name="classvideo" datasource="dsn">

    Do not forget to VAR scope all of the variables, including the two query names.

    how do I access them on the page that is calling it?

    The function returns a structure containing two keys:  "VideoList" and "aged240video". Just use the key names, like with any structure

                 #yourResultStructure.VideoList#

                 #yourResultStructure.aged240video#

    I am wanting to have a function return multiple queries?

    Just curious .. do you really need to return both queries from a single function call?

    Message was edited by: -==cfSearching==-