Flex and PHP Remoting: Typed Value Objects
After following a combination of Mihai Corlan's and Alan Gruskoff's excellent instructional posts on Zend AMF remoting with Flex, I was having some problems with returning typed objects from AMF into Flex.
The tear-your-hair-out-at-midnight-and-dream-about-running-away-and-opening-a-bar-in-Madagascar type of problems.
No matter what I did, AMF was returning generic Objects into Flex. I tried (what I thought was) everything. Octuple-checked my PHP value object classes, my PHP gateway, my PHP services, the metadata on my AS3 classes; nothing was working.
On a whim, manually imported the AS3 value object class into the test MXML component in which I was using the RemoteObject. No effect; still just getting arrays of Objects.
Finally, in desperation, I created an instance of the AS3 value object in the MXML component; I think at the time it was named "goToHellYouBastard," or something similar. It didn't do anything, wasn't getting sent any data, and served no function but to sit there and look enticing, like a judas goat for my AMF value objects.
Worked like a charm. Oodles of typed objects returning from AMF.
So, for Zend AMF to successfully map to a value object class in Flex, on top of the framework stuff you need to get them talking to each other, it appears that you require:
- IDENTICAL value object classes. If you have methods on your value objects, no go. You'll have to instantiate them as some other class, and recast or loop through the properties and transfer them over after they come into Flex. I'm still working on how to do this elegantly.
- A setClassMap for each mapping in your PHP gateway. Please note, it appears you don't need to add any path/package information for these classes; regardless of where your classes are, it appears that: $server->setClassMap("ValueObjectClass","ValueObjectClass"); will work fine.
- [Bindable] and [RemoteClass alias="ValueObjectClass"] metadata in your AS3 value object class.
- At least one instantiation of the AS3 value object class within the AS3 class from which you are doing the remoting calls.
For some reason I was under the impression that this wouldn't be necessary. I was going to just grab all my VOs in a service call and hork them off to a model somewhere for processing, but it seems like I need at least one instance to start with. At any rate, I wasn't able to find this specific information anywhere else (or at least, didn't notice it); hopefully this will be of help to someone.