AMFPHP ArrayCollection
AMFPHP does not natively convert Arrays to ArrayCollections, and there is no ArrayCollection class to speak of for PHP. You may sometimes need ArrayCollection typed values to be returned within a VO. There are three fixes that I’ve found for this.
Fix 1
If you are just expecting an ArrayCollection as the result of a PHP service call then simply create an ArrayCollection out of the returned Array from PHP. The following AS3 casts the returned array into an array collection.
private function result(data:Object):void{
var someDataProvider:ArrayCollection = new ArrayCollection(data.result);
}
Fix 2
If you need ArrayCollection values within a VO, rather than doing a simple cast (well, really its an instantiation), you should create the ArrayCollection type in PHP.
class ArrayCollection{
public $_explicitType = "flex.messaging.io.ArrayCollection";
public $source = array();
function ArrayCollection(){
$this->source = array();
}
}
?>
Then, whenever a value is expected to map to ArrayCollection within Flex, use ArrayCollection in PHP!
class SomeVO{
public $someArray = ArrayCollection();
...
$someArray->source[0] = "something";
}
?>
Fix 3
This fix is very quick and easy, but don’t expect it to work ALL the time. The idea is to throw your Array into a RecordSet constructor and return that instantiation in the service call. If your call is AMF3 it will return an ArrayCollection and AMF0 will return a RecordSet.
return new RecordSet($myArray);
This works great for returning associatively indexed values ([or] results from a database query), but straight up [0,1,2] Arrays will not work.
Thanks Chad Callahan for putting this cheat sheet together!


Hello Wade,
you can use this as an alternative :
class ArrayCollection extends ArrayObject
{
var $_explicitType = “flex.messaging.io.ArrayCollection”;
public function __construct( $config = array() )
{
parent::__construct( array(), ArrayObject::ARRAY_AS_PROPS );
}
}
and populate it directly like normal array.
$ac = new ArrayCollection();
$ac[ 0 ] = “something”;
cheers,
Adnan
Whoa I like that A LOT! Thanks Adnan!
You are welcome
It should be noted that the ArrayObject method only works in PHP5. So if you are still working with PHP4, which my company still has a lot of clients running, then the earlier method works.
For me I added a little something to it.
source = $array;
}
}
?>
This way I can pass the array into the ArrayCollection, so in PHP4 I can use an Array and then pass it into a new ArrayCollection when I’m done.
Sorry, my code got messed up:
class ArrayCollection{
var $_explicitType = “flex.messaging.io.ArrayCollection”;
var $source = array();
function ArrayCollection( $array )
{
if( !isset( $array ) )
{
$array = array();
}
$this->source = $array;
}
}
my 2 cents….
I like this better:
class ArrayCollection
{
public $_explicitType = “flex.messaging.io.ArrayCollection”;
public $source = array();
public function ArrayCollection( $arr = null )
{
if ( isset($arr) ) $this->source = $arr;
}
public function addItem( $o ) {
$this->source[] = $o;
}
}
I’ve been scouring the web to figure out how to get an array collection from a MySQL database using AMFPHP.
On your blog you mention:
“This is trivial in Flex…First AMFPHP returns all AMF3 result resources as an ArrayCollection….”
Here tho you write:
“AMFPHP does not natively convert Arrays to ArrayCollections”
What is the best way you consider then. Are the techniques in this post solid? When I return a mysql query like:
$dbc = mysql_connect(”localhost”,”root”,”root”);
mysql_select_db(”bssloc”);
$result = mysql_query(”SELECT * FROM languages”);
return $result;
In the AMFPHP browser Ill get:
(mx.collections::ArrayCollection)#0
filterFunction = (null)
length = 8
list = (mx.collections::ArrayList)#1
length = 8
source = (Array)#2
[0] (Object)#3
code = “ITA”
eng_name = “english word”
id = 2
local_name = “itallian werd”
[1] (Object)#4
code = “FRA”
eng_name = “Man”
id = 3
local_name = “Homme”
[2] (Object)#5
code = “ESP”
eng_name = “Cervesa”
id = 4
local_name = “Beer”
[3] (Object)#6
code = “PTB”
eng_name = “Party”
id = 5
local_name = “el party”
uid = “9E53BDDA-31EA-75AE-8670-F668EEA1CFA1″
sort = (null)
source = (Array)#2
This looks like an arrayCollection, what is going on. I’m trying this PHP arraycolleciton class you;ve got here, but I think it will take me a bit to crack it.
Any advice…points into the right direction….
I was looking at the option:
Hello Wade,
you can use this as an alternative :
class ArrayCollection extends ArrayObject
{
var $_explicitType = “flex.messaging.io.ArrayCollection”;
public function __construct( $config = array() )
{
parent::__construct( array(), ArrayObject::ARRAY_AS_PROPS );
}
}
and populate it directly like normal array.
$ac = new ArrayCollection();
$ac[ 0 ] = “something”;
Does anyone have a small sample in a completed (test or release) app?
To add to the above, but i was just hoping to see it in context to an application.
Hi guys. I’m working with Flex 3, PHP 5.x (can’t remember exactly) and Weborb. After reading the article and all your comments (thanks a lot by the way), I came with something very simple that interfaces perfectly with Flex and added some basic features:
—————————————————-
class ArrayCollection {
public $source = array();
public function __construct($arr = null) {
if(isset($arr))
$this->source = $arr;
else {
$this->source = array();
}
}
public function push($o) {
array_push($this->source, $o);
}
public function addItem($index, $item) {
$this->source[$index] = $item;
}
}
—————————————————-
It would, of course, be a good thing to add function to remove objects from the array, but I hope it will help some visitors.
Hello,
thank you for this post.
I think a “new” is missing in your code example.
It should be:
class SomeVO{
public $someArray = new ArrayCollection();
}
Above you mentioned correctly that the following only works for associative arrays.
return new RecordSet($myArray);
A very simple construct that works for “straight up arrays” like array(”A”,”B”,”C”) is
return new ArrayObject($myArray);
This will nicely return as an ArrayCollection.
Of course same works for returning ArrayCollection of VO’s (even without the need for class definitions of the vo’s in php – like shown below).
public function getUsers() {
$users[] = (object) array(’_explicitType’ => ‘User’, ‘name’ => ‘User1′);
$users[] = (object) array(’_explicitType’ => ‘User’, ‘name’ => ‘User3′);
$users[] = (object) array(’_explicitType’ => ‘User’, ‘name’ => ‘User3′);
$userCollection = new ArrayObject($users);
return($userCollection);
}
Call this in amfphp and it will return an ArrayCollection of 3 User Objects, without even a User class definiton in php.
– Geert
I just did this from my event result in my remote object handler
_arr2:ArrayCollectionExtended = new ArrayCollectionExtended(ArrayUtil.toArray(event.result));
@travis
Sorry I screwed up that won’t work
_arr2:ArrayCollectionExtended = ArrayCollectionExtended(event.result);
this might work better i no it works for standard ArrayCollection
Hey guys,
Fix #2 is giving me a PHP parse error at public $someArray = ArrayCollection(); saying that a ; or , is expected…
How did you get your example to work properly without the error?
Sorry to post again right away, but does this remedy mapping arraycollections from Flex to PHP as well?
Hi,
is there anybody who did this example to work, I’ve some problems by
using an ArrayCollection inside a VO.
Is there complete example code out there ?