arrayOfObjectsToQuery(theArray)
Last updated June 11, 2009
Version: 0 | Requires: CF6 | Library: DataManipulationLib
Description:
Converts an array of objects to a CF Query Object. Only works with "Bean" style CFCs. Uses the result of all getX methods to populate the query.
Based on arrayOfStructuresToQuery by David Crawford http://cflib.org/udf/ArrayOfStructuresToQuery
Return Values:
Returns a query.
Example:
<cfset test1 = CreateObject('component','someObj').init( 'foo', 'bar' ) />
<cfset test2 = CreateObject('component','someObj').init( 'foo1', 'bar1' ) />
<cfset myArray = ArrayNew(1) />
<cfset arrayAppend(myArray,test1) />
<cfset arrayAppend(myArray,test2) />
<cfset QoQ = arrayOfObjectsToQuery(myArray)>
<cfdump var="#QoQ#" />
Parameters:
Name | Description | Required |
---|---|---|
theArray | The array of CFCs. | Yes |
Full UDF Source:
/**
* Converts an array of objects to a CF Query Object.
*
* @param theArray The array of CFCs. (Required)
* @return Returns a query.
* @author Don Quist (don.sigmaprojects@gmail.com)
* @version 0, June 11, 2009
*/
function arrayOfObjectsToQuery(theArray){
var colNames = ArrayNew(1);
var theQuery = queryNew("");
var i=0;
var j=0;
var o=0;
var functions = '';
//if there's nothing in the array, return the empty query
if(NOT arrayLen(theArray)) return theQuery;
//get meta data for the first object in the array and set the functions
functions = getMetaData(theArray[1]).functions;
//get the column names into an array =
for(o=1; o LTE arrayLen(functions); o=o+1){
if( REFindNoCase( 'get+', functions[o].NAME ) and functions[o].NAME IS NOT 'init' ) {
arrayAppend(colNames, LCase(REReplaceNoCase(functions[o].NAME, "^get",'' )) );
}
}
theQuery = queryNew(arrayToList(colNames));
//add the right number of rows to the query
queryAddRow(theQuery, arrayLen(theArray));
//for each element in the array, loop through the columns, populating the query
for(i=1; i LTE arrayLen(theArray); i=i+1){
for(j=1; j LTE arrayLen(colNames); j=j+1){
//bug out incase something isnt defined in the object
try {
querySetCell(theQuery, colNames[j], Evaluate('theArray[i].get#colNames[j]#()'), i);
}
catch(Any excpt) { }
}
}
return theQuery;
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016