从cfc返回多个存储过程结果集

Rob*_*b M 3 coldfusion cfc

我试图将我的应用程序中的某些页面转换为使用cfc,并且一个页面使用存储过程来检索几组数据.

现在,当我访问结果时,它们的行为就像我使用了一个<cfquery>标签,以及所有的功能.所以现在我正在尝试在我正在构建的cfc中使用相同的存储过程,并且我希望能够以相同的方式访问结果,并且存在我的问题.我不知道如何从函数返回多个查询,而不创建我已经开始的数组.顺便说一句,功能是不完整的.我只想努力工作.在下面的设置中,我得到了一个查询对象数组,但我觉得有更好的方法可以做到这一点.

这是<cffuntion>:

<cffunction name="getProfileData" 
            access="public" 
            output="false" 
            returntype="string">

    <cfargument name="cusip" type="string" required="true">
    <cfargument name="report_date" type="date" required="true">
    <cfset var errorMessage = "everything is good">

    <cftry>
        <cfstoredproc datasource="#dsn#" procedure="prc_asset_profile_retrieve">
            <cfprocparam type="in" cfsqltype="cf_sql_varchar" value="#cusip#" dbvarname="@cusip">
            <cfprocparam type="in" cfsqltype="cf_sql_varchar" value="#report_date#" dbvarname="@reportDate">
            <cfprocresult name="profile_head" resultset="1">
            <cfprocresult name="attribution" resultset="2">
            <cfprocresult name="characteristics" resultset="3">
            <cfprocresult name="exposure" resultset="4">
            <cfprocresult name="weights" resultset="5">
            <cfprocresult name="holdings" resultset="6">
        </cfstoredproc>

        <cfset var profileArray = []>
        <cfset #ArrayAppend(profileArray,profile_head)#>

        <cfcatch type="any">
            <cfset errorMessage = "something happened">
        </cfcatch>          
    </cftry>

    <cfreturn profileArray>
</cffunction>
Run Code Online (Sandbox Code Playgroud)

当我输出一些测试数据时,它会匹配

<cfset count = fund_profile.getProfileData("#cusip#","#report_date#")> 
<cfdump var="#count[1]#">
<cfoutput>
    From cfc (##count[1].recordCount##): #count[1].recordCount#<br>
    From stored proc (##profile_head.recordCount##): #profile_head.recordCount#
</cfoutput>
Run Code Online (Sandbox Code Playgroud)

我明白了:

来自cfc(#count [1] .recordCount#):1
来自存储过程(#profile_head.recordCount#):1

但第二种方式看起来更清洁.

   -----------------------------WORKING SOLUTION------------------------------ 
Run Code Online (Sandbox Code Playgroud)

所以在使用@leigh的答案后,我想出了这个.

这是完整的cfc:

<cfcomponent displayname="Fund Profile" hint="This is the cfc that will do the processing of all fund profile information" output="false">
     <cfproperty name = "result1"> <!--- PROFILE HEAD --->
     <cfproperty name = "result2"> <!--- ATTRIBUTION --->
     <cfproperty name = "result3"> <!--- CHARACTERISTICS --->
     <cfproperty name = "result4"> <!--- EXPOSURE --->
     <cfproperty name = "result5"> <!--- WEIGHTS --->
     <cfproperty name = "result6"> <!--- HOLDINGS --->

     <cffunction name="init" 
            displayname="init" 
            hint="This will initialize the object" 
            access="public" 
            output="false" 
            returnType="Any">

        <cfargument name="dsn"  type="string" required="true" />
        <cfargument name="cusip" type="string" required="true" />
        <cfargument name="report_date" type="date" required="true" />

        <cfset variables.dsn = #arguments.dsn#>
        <cfset variables.cusip = #arguments.cusip#>
        <cfset variables.report_date = #arguments.report_date#>

        <cfscript>
            getProfiledata(cusip,report_date);
        </cfscript>     

        <cfreturn this>
    </cffunction>

    <cffunction name="getProfileData" 
            access="private" 
            output="false" 
            returntype="void">

        <cfargument name="cusip" type="string" required="true">
        <cfargument name="report_date" type="date" required="true">

        <cfstoredproc datasource="#dsn#" procedure="prc_asset_profile_retrieve">
             <!--- STORED PROCEDURE HASN'T CHANGED.  SEE ABOVE FOR CODE --->
        </cfstoredproc>

        <cfscript>
            setProfilehead(profile_head);
            setAttribution(attribution);
            setCharacteristics(characteristics);
            setExposure(exposure);
            setWeights(weights);
            setHoldings(holdings);
        </cfscript>

        <cfreturn>
    </cffunction>

    <!--- NOT GOING TO INCLUDE ALL SETTERS AND GETTERS, --->
    <!--- BECAUSE THEY ARE ALL THE SAME OTHER THAN THE NAMES --->

    <cffunction name="setProfileHead" access="private">
        <cfargument name="ProfileHead">
        <cfset variables.result1 = arguments.ProfileHead>       
    </cffunction>

    <cffunction name="getProfileHead" access="public" returntype="query">
        <cfreturn variables.result1>
    </cffunction>

</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

以下是调用页面中的代码:

<cfset fund_profile = CreateObject("component", "CFCs.fund_profile").init("#dsn#","#cusip#","#report_date#")>
<cfset profile_head = fund_profile.getProfileHead()>
Run Code Online (Sandbox Code Playgroud)

对不起所有的代码,但我想让代码可用.所以有人看到我提出的任何问题吗?

Lei*_*igh 8

函数只能返回单个值.如果您希望返回多个值,则需要使用某种类型的复杂对象(数组,结构......)如果数组不够直观,您可以将查询放在结构中并返回它.然后调用页面可以按名称而不是索引访问查询.

(注意,请务必正确调整范围/本地化所有函数变量.)

 <cfset var data = {}>
 ...
 <!--- store query results in structure --->
 <cfset data.profile_head = profile_head>
 <cfset data.attribution = attribution>
 ... 
 <cfset data.holdings = holdings>
 <!--- return structure --->
 <cfreturn data>
Run Code Online (Sandbox Code Playgroud)