How to retrieve result from an remoteobject AMFPHP Flex 4.5

1.3k views Asked by At

I have a quick question about flex 4 remoteObjects. I would like to retrieve information from a MySql DB via amfphp to Flex 4.5. I'm using a remoteobject tag. I would like to use the result attribute but it doesn't seem to work for me. What am i doing wrong?

If i collect the information form the DB without a resulthandler it works fine, but when i would like to collect the informatie in an arraycollection it doesn't work. The arraycollection never gets filled with the information i retrieve.

This works;

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600"
           creationComplete="initApp()">

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <mx:RemoteObject id="myRemote" 
                     destination="solicitantService" 
                     source="resume.solicitantService"   
                     endpoint="http://localhost:8181/amfphp/gateway.php"/>
</fx:Declarations>

<fx:Script>
    <![CDATA[

        private function initApp():void
        {
            myRemote.getUsers();
        }

    ]]>
</fx:Script>

<mx:DataGrid id="myGrid" dataProvider="{myRemote.getUsers.lastResult}"/>    
</s:Application>

and this doens't work.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600"
           creationComplete="initApp()">

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <mx:RemoteObject id="myRemote" 
                     destination="solicitantService" 
                     source="resume.solicitantService"   
                     endpoint="http://localhost:8181/amfphp/gateway.php"
                     result="myRemote_resultHandler(event)"/>
</fx:Declarations>


<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.rpc.events.ResultEvent;

        [Bindable]
        private var users:ArrayCollection = new ArrayCollection();


        private function initApp():void
        {
            myRemote.getUsers();
        }

        protected function myRemote_resultHandler(event:ResultEvent):void
        {
            users = event.result as ArrayCollection;
        }

    ]]>
</fx:Script>

<mx:DataGrid id="myGrid" dataProvider="{users}"/>
</s:Application>

what am i doing wrong? can anybody help met out on this one? i have tried it with both the spark and the mx datagrid.

Well i have found the solution. From Php i revieve an Array not an ArrayCollection.

2

There are 2 answers

0
Roger On

amfPHP doesn't return results as an ArrayCollection, but rather an Array. Well done on figuring that part out.

Here is a link to some code that really helped me. It starts off with basic strings, then objects then array (of objects).

http://www.brentknigge.com/?q=node/499

0
Anas Bin Numan On

This is because you are assigning an array to an array collection.

it's tough to answer accurately without having your php function insight. If your php service is returning something like this:

$outputArray['users'] = myUsers(); //here myUsers() is a function which is doing the query and fetching the results

you can put it in an array collection like this:

var usersCollection:ArrayCollection = new ArrayCollection(event.result.usres);

Hope it helps