自定义函数在ColdFusion 10/11中有效但在Lucee

时间:2018-02-02 22:19:55

标签: coldfusion railo lucee

我有以下功能,继承了一些CF代码。它有效,但仅限于ColdFusion版本10和11.我不关心2016年没有升级计划。我试图在Lucee下运行它,但它不起作用。

<cffunction name="QueryToArray" access="public" returntype="array" output="false" hint="This turns a query into an array of structures.">
  <!--- Define arguments. --->
  <cfargument name="Data" type="query" required="yes" />
  <cfscript>    
    var LOCAL = StructNew(); // Define the local scope.
    LOCAL.Columns = data.getMetaData().getColumnLabels();  // Get the column names as an array.
    LOCAL.QueryArray = ArrayNew(1); // Create an array that will hold the query equivalent.
    for (LOCAL.RowIndex = 1 ; LOCAL.RowIndex LTE ARGUMENTS.Data.RecordCount; 
    LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
        LOCAL.Row = StructNew();
        for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE ArrayLen(LOCAL.Columns); 
        LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
            LOCAL.ColumnName = LOCAL.Columns[LOCAL.ColumnIndex];
            LOCAL.Row[LOCAL.ColumnName] = ARGUMENTS.Data[LOCAL.ColumnName][LOCAL.RowIndex];
        }
        ArrayAppend(LOCAL.QueryArray, LOCAL.Row);
    }
    return(LOCAL.QueryArray);
    </cfscript>
</cffunction>

在Lucee,我尝试做出这些改变:

<cffunction name="QueryToArray" access="public" returntype="array" output="false" hint="This turns a query into an array of structures.">
  <!--- Define arguments. --->
  <cfargument name="Data" type="query" required="yes" />
  <cfscript>    
    var LOCAL = StructNew(); // Define the local scope.
    LOCAL.Columns = data.getColumnlist(false); // Get the column names as an array.
    LOCAL.QueryArray = ArrayNew(1); // Create an array that will hold the query equivalent.
    for (LOCAL.RowIndex = 1 ; LOCAL.RowIndex LTE ARGUMENTS.Data.RecordCount; 
    LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
        LOCAL.Row = StructNew();
        for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE ArrayLen(LOCAL.Columns); 
        LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
            LOCAL.ColumnName = LOCAL.Columns[LOCAL.ColumnIndex];
            LOCAL.Row[LOCAL.ColumnName] = ARGUMENTS.Data[LOCAL.ColumnName][LOCAL.RowIndex];
        }
        ArrayAppend(LOCAL.QueryArray, LOCAL.Row);
    }
    return(LOCAL.QueryArray);
    </cfscript>
</cffunction> 

但是我收到了这个错误:

  

消息:无法将String [name,value]转换为[Array]类型的值
  详细信息:对象的Java类型是java.lang.String
  的堆栈跟踪:    

中发生错误
C:\home\website\wwwroot\controller\production\cfc\kernel.cfc: line 12 
     10: LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
     11: LOCAL.Row = StructNew();
     12: for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE ArrayLen(LOCAL.Columns); 
     13: LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
     14: LOCAL.ColumnName = LOCAL.Columns[LOCAL.ColumnIndex];

1 个答案:

答案 0 :(得分:4)

要让您的代码正常工作,您需要做的就是从

更改第6行
LOCAL.Columns = data.getColumnlist(false); // Get the column names as an array.

到这个

LOCAL.Columns = data.getColumnNames(); // Get the column names as an array.

但是我找到了一个在ColdFusion和Lucee都有效的要点here。我决定创建一些示例代码和test it at https://trycf.com/。我确认它适用于Lucee 4.5和5.它也适用于ColdFusion 10,11,2016。希望这有帮助!

<!--- Create a new three-column query, specifying the column data types ---> 
<cfset myQuery = QueryNew("Name, Time, Advanced", "VarChar, Time, Bit")> 

<!--- Make two rows in the query ---> 
<cfset QueryAddRow(MyQuery, 2)> 

<!--- Set the values of the cells in the query ---> 
<cfset QuerySetCell(myQuery, "Name", "The Wonderful World of CMFL", 1)> 
<cfset QuerySetCell(myQuery, "Time", "9:15 AM", 1)> 
<cfset QuerySetCell(myQuery, "Advanced", False, 1)> 
<cfset QuerySetCell(myQuery, "Name", "CFCs for Enterprise 
        Applications", 2)> 
<cfset QuerySetCell(myQuery, "Time", "12:15 PM", 2)> 
<cfset QuerySetCell(myQuery, "Advanced", True, 2)> 

<h4>The query</h4> 
<cfdump var="#myQuery#">

<h4>The array of objects</h4> 
<cfset myArray = QueryToArray(myQuery)>
<cfdump var="#myArray#">

<cfscript>
/**
* @hint Returns reasonable array of objects from a cfquery
*/
public function queryToArray(
    required query  query,
             string excludeColumns = ""
){

    if (server.coldfusion.productName == "ColdFusion Server") {
        local.columns = arguments.query.getMetaData().getColumnLabels();
    } else if (server.coldfusion.productName == "Lucee") {
        local.columns = arguments.query.getColumnNames();
    } else {
        local.columns = [];
    }

    local.response = arrayNew(1);

    for (local.rowIndex=1; local.rowIndex<=arguments.query.recordCount; local.rowIndex++) {
        local.response[local.rowIndex] = structNew();

        for (local.columnIndex=1; local.columnIndex<=arrayLen(local.columns); local.columnIndex++) {
            local.columnName = local.columns[local.columnIndex];

            if(local.columnName != "" && (arguments.excludeColumns == "" || !listFindNoCase(arguments.excludeColumns, local.columnName))) {
                local.response[local.rowIndex][local.columnName] = arguments.query[local.columnName][local.rowIndex];
            }
        }
    }

    return local.response;
}
</cfscript>