Skip to main content
Inspiring
September 15, 2009
Question

How to add an Object to an Array?

  • September 15, 2009
  • 2 replies
  • 1084 views

Hello, I am new to Coldfusion, so I may be going down a wrong road to solve this problem of mine.  The problem is when I invoke a CFC function with a returntype set to Query, the property names in the returned array are all uppercase.  But when I invoke a CFC function with a custom datatype, the property name's case are preserved to camelCase.  And when I deserialize the object in a Flex app, I want to use the same value object without using a conditional statement to figure out what function the object is coming from in the CFC.

So I decided that I should do a query (step 1), declare an array (step 2), and then populate the new array with the values in from the query (step 3).  However I can't find a way to attach properties to an Array.  Hopefully the code below will highlight my attempt.  The code in red, is giving me an error.

If you understand my goal and know of a more ideal way to accomplish this problem, please post.

Thanks.

   

<cffunction name="getUsers" returntype="Array" access="remote">


        <!--- 1. Do the query --->
        <cfquery name="qUsers" datasource="#dsn#">
           SELECT *
           FROM Users
        </cfquery>
       
        <!--- 2. Declare the array --->
        <cfset userListArray = ArrayNew(2)>
       
        <!--- 3. Populate the array row by row --->
        <cfloop query="qUsers">
           <cfset userListArray[CurrentRow]["firstName"]         = FIRSTNAME>
           <cfset userListArray[CurrentRow]["lastName"]         = LASTNAME>

        </cfloop>
       
        <cfreturn userListArray />
       

</cffunction>

    This topic has been closed for replies.

    2 replies

    Inspiring
    September 18, 2009

    ColdFusion "structs" are associative arrays ... and can, in fact, be referenced using either "object.element" or "object['element']" notation.

    You can mix arrays and structs with ease, and you'll find that it's passing around references to things as most languages do (including the underlying Java).

    You'll find that the notation is not quite as easy as you might like it to be.  For example, while you can declare an array with "[ ]" list-notation, or a struct with "{ }" notation, you can't in one step specify an array of structs or a struct of arrays.

    Inspiring
    September 15, 2009

    Flex might have a concept of an associative array - wherein elements can have text labels, rather than numeric indexes; but CF does not.  In CF one needs to use a struct for that; same thing, different name.

    So for your requirement, you need to use an array of structs, not a two dimensional array.

    I don't know how well this will gel with Flex's requirements.

    --

    Adam

    injpixAuthor
    Inspiring
    September 18, 2009

    Ok, thanks for the info!  So created the code below a few days ago; but had a real hard to debug Coldfusion with Flex.  So I lost several hours doing just that, otherwise I would have posted earlier.

    Now, the problem is that memberListArray gets returned with no elements.  I see that it gets populated (with the structures) by having it get dumped into a log file.  Is <cfset> and/or <cfreturn> the correct tags to use for this?


        <cffunction name="getMembers" returntype="Array" access="remote">
           
            <!--- Do the query --->
            <cfquery name="qMembers" datasource="#dsn#">
               SELECT *
               FROM Members
            </cfquery>


            <!--- Declare the array --->
            <cfset memberListArray = ArrayNew(1)>


            <!--- Populate the array row by row --->
            <cfloop query="qMembers">
                <cfscript>
                    member = StructNew();
                    StructInsert(member, "firstName",     FIRSTNAME);
                    StructInsert(member, "lastName",     LASTNAME);
                    StructInsert(member, "emailAddress",EMAILADDRESS);
                    StructInsert(member, "uid",         UID);
                   
                    ArrayAppend(memberListArray, member);
                   
                </cfscript>
            </cfloop>

            <cfdump var="#memberListArray#" output="C:\Coldfusion9\logs\cfdump.log">


            <cfreturn memberListArray />
           
        </cffunction>

    injpixAuthor
    Inspiring
    September 18, 2009

    I got it fixed.


    My problem was with the object I was Iooking at in Flex.  Prior, I was using the 'event.token.result.source' object from the AsyncToken.  Now, I am just using the 'event.result' object.

    Thanks!