anyone know of a quick way to turn a large array i...
# cfml-general
c
anyone know of a quick way to turn a large array into a query object?
Oh I believe that is struct to query...prob need this first then: https://cfdocs.org/arraytostruct
c
ah i see why that wasn't working i have a 2d array
g
I did something like this once for ACF. The function will accept the array of structs (data), and if you specify the list of columns (id, firstName, lastName, etc) and datatypes (integer, varchar, varchar, etc), it will create the query with that, otherwise CF will just guess the type.
Copy code
public query function arrayToQuery( required array data, string columnList="", string columnTypeList="" ){
		if( arguments.columnList.trim().len() && arguments.columnTypeList.trim().len() ){
			return queryNew( arguments.columnList, arguments.columnTypeList, arguments.data );
		} else {
			return queryNew( arguments.data );
		}
	}
👍 1
❤️ 1
Here is an example you can run on trycf.com
Copy code
<cfscript>
    data = [
        { id: 1, label: "One" },
        { id: 2, label: "Two" },
        { id: 3, label: "Three" },
        { id: 4, label: "Four" }
    ];

    writedump( arrayToQuery( data, "id,label", "integer,varchar" ) );
    writedump( arrayToQuery( data ) );


	public query function arrayToQuery( required array data, string columnList="", string columnTypeList="" ){
		if( arguments.columnList.trim().len() && arguments.columnTypeList.trim().len() ){
			return queryNew( arguments.columnList, arguments.columnTypeList, arguments.data );
		} else {
			return queryNew( arguments.data );
		}
	}

</cfscript>
If you are using Lucee, the first option passing in the columns and types should work. If you just pass in the array, that doesn't seem to work as expected.
its really just using
queryNew()
🙂
d
Don't know if it applies, but in ACF 10+ I think you can pass an array of structs to queryAddRow(), to add multiple rows.
d
Out of curiosity @Cory Howitz, why do you need a query instead of an array?
c
downstream code does qoq on it.
👍 1