Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
Sending an ArrayCollection – Flex to amfphp
Here are my tweaks to amfphp for sending an ArrayCollection from Flex to amfphp. May not be the cleanest or most proper but it works. From what I understand, there is an ArrayCollection class included in Zend_AMF, but I haven't looked into that.
I've been sending ArrayCollections from php to Flex for a while, it's pretty simple and info for doing that can be found at Wade Arnold's blog here. The issue going the other direction is that Flex only sends the AC's underlying source array when serializing it for amf. So when amfphp unpacks the data, you're dealing with a normal array.
Obviously, the first thing you'll need is your ArrayCollection class to use (I use the version that extends ArrayObject at the link above), and make sure it is in your include path.
The first tweak to amfphp is to the method AMFBaseDeserializer->mapClass(). In that method, right after the checks looking for CommandMessage and RemotingMessage types, I added a similar check for ArrayCollection:
-
if($typeIdentifier == "flex.messaging.io.ArrayCollection")
-
{
-
return new ArrayCollection();
-
}
Next change is to the AMFDeserializer->readAmf3Object() method. Near the middle of the method, around line 570, there is a check for ArrayCollection, and if found it will call the readAmf3Data() method. I changed it to pass in a value of "true" to the method, you'll see why in a sec:
-
if( $classDefinition['externalizable'] )
-
{
-
if($type == 'flex.messaging.io.ArrayCollection')
-
{
-
$obj = $this->readAmf3Data(true);
-
}
Now just two small tweaks to the readAmf3Data() method. I changed the method signature to add an $isArrayCollection parameter, which just gets forwarded on when calling readAmf3Array. Here's the whole method:
-
function readAmf3Data($isArrayCollection = false)
-
{
-
$type = $this->readByte();
-
-
switch($type)
-
{
-
case 0x00 : return null; //undefined
-
case 0x01 : return null; //null
-
case 0x02 : return false; //boolean false
-
case 0x03 : return true; //boolean true
-
case 0x04 : return $this->readAmf3Int();
-
case 0x05 : return $this->readDouble();
-
case 0x06 : return $this->readAmf3String();
-
case 0x07 : return $this->readAmf3XmlString();
-
case 0x08 : return $this->readAmf3Date();
-
case 0x09 : return $this->readAmf3Array($isArrayCollection);
-
case 0x0A : return $this->readAmf3Object();
-
case 0x0B : return $this->readAmf3XmlString();
-
case 0x0C : return $this->readAmf3ByteArray();
-
}
-
}
Now just another two small tweaks to the readAmf3Array method. As with the readAmf3Data() method, we add the $isArrayCollection parameter. Within the body of the method, I just check if that param is true when creating the $hashtable variable, and if it is, use an ArrayCollection instead of a regular array:
-
function readAmf3Array($isArrayCollection = false)
-
{
-
$handle = $this->readAmf3Int();
-
$inline = (($handle & 1) != 0 ); $handle = $handle>> 1;
-
-
if( $inline )
-
{
-
-
$this->storedObjects[] = & $hashtable;
-
$key = $this->readAmf3String();
-
while( $key != "" )
-
{
-
$value = $this->readAmf3Data();
-
$hashtable[$key] = $value;
-
$key = $this->readAmf3String();
-
}
-
-
for($i = 0; $i <$handle; $i++)
-
{
-
//Grab the type for each element.
-
$value = $this->readAmf3Data();
-
$hashtable[$i] = $value;
-
}
-
return $hashtable;
-
}
-
else
-
{
-
return $this->storedObjects[$handle];
-
}
-
}
That's it. Now I can send the an AC to the server, serialize it, unserialize it and send it back to Flex with no issues. The one thing that may an issue is if one of the items within an AC is also another AC...haven't checked out if that works yet.
| Print article | This entry was posted by rich on November 20, 2009 at 6:00 am, and is filed under Flex, php. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 3 months ago
This is brilliant work, thankyou very much! Its this kind of work that will keep AMFPHP alive in the face of ZendAMF
about 1 month ago
Can someone please help me. I can;t for the life of me figure how to get an object with an arrayCollection as one of its properties sent to a service from flex and back again to flex. I can get the value of the non-array collection but I cant seem to get the values of the arraycollection property. Please email me at albertanddelia@hotmail.com if you are willing to help. Thanks.