arrayOfObjectsToQuery(theArray)
Last updated June 11, 2009
Version: 0 | Requires: ColdFusion MX | 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 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:
<cfscript>
/**
* 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;
}
</cfscript>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
11 day(s) ago
Will Belden added
longTime
17 day(s) ago
James Sleeman added
quickSort
27 day(s) ago
Ben Forta added
GetHostAddress
30 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)