views:

831

answers:

1

I currently have an arrayCollection in Flex and I want to sent it to PHP (Zend_AMF). According to the Zend_AMF wiki, sending an arrayCollection over directly will force Zend_AMF to cast the arrayCollection as an object which is no good. I'd rather have an array of my models.

I assume the best way would be to convert the arrayCollection to an array in flex and then send it over. Is this true, and if so how would I do that in Flex 3?

If you have a better recommendation, that would be appreciated as well.

Thanks for looking!

+1  A: 

Actually, you can create an ArrayCollection type on the PHP side and send native ArrayCollection objects directly over AMF.

Here is some php code I have that works. Save this in a file called

ArrayCollection.php

<?php
class ArrayCollection {
    public function getASClassName()
    {
        return 'flex.messaging.io.ArrayCollection';
    }

    var $source = array();

    function ArrayCollection()
    { 
     $this->source = array(); 
    }
}

To use this on the php side include the ArrayCollection.php in your php project and the syntax to call it looks something like this:

$myArrayCollection = new ArrayCollection();

and if you want to access the array that composes the ArrayCollection you can do this

$someArray = $myArrayCollection->source;

On the Flex side you can pass Array Collections directly to the server over Zend AMF. In one of my projects I have many value objects that have ArrayCollections in them and they work just fine on the PHP side. So it can be done.

If you absolutely can't get the ArrayCollection working in PHP you can just access the array as the "source" property of the ArrayCollection in Actionscript. The code looks something like this in actionscript:

import mx.collections.ArrayCollection;

public var myAC:ArrayCollection = new ArrayCollection();

public var myArray:Array = new Array();

// populate your ArrayCollection with data...

myArray = myAC.source;

myArray will now be an Array of the objects in the ArrayCollection myAC.

Hope this helps. If you have further questions and/or have a code sample let me know.

It took me a bit to figure this one out too.

Gordon Potter