views:

521

answers:

3

I have a datagrid which is editable and I need to send that back to the database via a CFC for insertion into the database after all the editing is complete. Dumping the array collection to cfdump tells me that I have an array with items and a structure but i cannot understand how to "loop" through each and insert into the DB.

There seems to be very little written which actually works! My MXML and CFC are below but give me the error of "You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members." -which is nice

all help is much appreciated - thanks

[Bindable]
private var getconsent:ArrayCollection = new ArrayCollection([
{LocationName:'Service A', Contact: 'Bob Jones' },  
{LocationName:'Service B', Contact: 'Jane Smith' },
{LocationName:'Service c', Contact: 'Doug Johnson' },   
{LocationName:'Service d', Contact: 'John Jackson'}   
]);  

public function send():void {  
cfdata.sendData(getconsent.source);  
}  

public function send_Result(event:ResultEvent):void {  
Alert.show('ok'); 
}  

public function send_Fault(event:FaultEvent):void {  
Alert.show(event.fault.message);  
} 

]]>  
</mx:Script>  

<mx:RemoteObject 
id="cfdata" 
showBusyCursor="true" 
destination="ColdFusion"  
source="buildtest.test2">  

<mx:method name="sendData" result="send_Result(event)" fault="send_Fault(event)" />  
</mx:RemoteObject>  

<mx:DataGrid id="myGrid" 
dataProvider="{getconsent}" editable="true" > 
<mx:columns>
<mx:DataGridColumn dataField="LocationName" width="150"
editable="false"/>

<mx:DataGridColumn dataField="Contact" width="150" /> 

</mx:columns> 
</mx:DataGrid> 
<mx:Button label="Update DB" click="send()"/>


<cfcomponent displayname="sendData" output="false" >  
    <cffunction name="sendData" access="remote"  output="no" returnType="void"          required="yes" >  

        <cfargument name="getconsent" type="any" required="true">  

        <cfloop from="1" to="#ArrayLen(getconsent.dataprovider)#" index="i">  

            <cfquery name="clientconsent" datasource="gvr">  

                INSERT INTO ClientConsent"
                        (Location)
                VALUES
                ('#getconsent.dataprovider.LocationName[i]#')

            </cfquery>
        </cfloop>
    </cffunction>

</cfcomponent>

array 1 struct Contact Bob Jones
LocationName Service A
mx_internal_uid 807D204F-A315-7D78-C745-BAD78087CB28

 2 struct 
 Contact     Jane Smith  
 LocationName   Service B  
 mx_internal_uid    EAA43EF4-A7EA-82C9-5F3C-BAD780D7FD6F  

 3 struct 
 Contact     Doug Johnson  
 LocationName   Service c  
 mx_internal_uid    9768D6D2-8F97-5F4D-767C-BAD780D7B478
+1  A: 

If you're using CF9, try DCD with Flex 4: http://ria.dzone.com/articles/flash-remoting-and-coldfusion

If you're using CF8 with Flex 3, try LCDS: http://www.adobe.com/devnet/coldfusion/articles/data%5Fapp.html

Henry
Thanks for the point on putting the cfloop inside the query i will do that (school boy error i guess ;)However i am not using LCDS just a straight connection from flex 3 to cf7 to SQLExpress DB- this should be easy but I just seem to be be missing something obvious.
charlie
I have added the cfdump to the original question - the bit that puzzles me is that it is an array, with a structure, with members and this is what coldfusion is complaining about - but thats what an array is????
charlie
So you're getting an array of struct, try #getconsent.dataprovider[i].LocationName# ?
Henry
A: 

Would it be easier for you to work with a query object instead of an array of structs? I created a UDF to convert an ArrayCollection to a Query [ArrayCollectionToQuery] in CF after it's returned from your Flex application.

Adam Tuttle
A: 

Hi Adam thanks for this it looks very useful, however i am not sure how to use it to insert data into my DB

<cfquery name="clientconsent" datasource="gvr">
INSERT INTO dbo.ClientConsent
(Location, ClientAppointments, ClientDemographics)
VALUES(
      #qresult#
)
</cfquery>
charlie