– Common Function Library Project


Last updated June 11, 2009


Don Quist

Version: 0 | Requires: CF6 | Library: DataManipulationLib

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

Return Values:
Returns a query.


<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#" />


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 ( 
 * @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;
blog comments powered by Disqus


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson