Passing VO's from PHP seems to be very well covered in numerous articles on the web, and it involves mainly adding a var $_explicitType="tutorials.Person" to the php VO, with a value that corresponds to the [RemoteClass(alias="tutorials.Person")] in the Flex VO. Pretty straightforward and foolproof. But no one seems to spend any time talking about going back the other way. This line states that our VOs should be placed in a folder named "vo" which should be inside the services folder. I found this little tidbit of information: "As for incoming class mapping, remember that if amfphp doesn't find the class to be mapped in the services/vo folder, it will simply deserialize the object as though it were untyped, that is, as an associative array. " It took a little bit of playing around but ultimately, without making any changes to the core AMFPHP files, I was able to get the reverse mapping working successfully. This is what that looked like:
Generally when a VO is passed from Flex to AMFPHP, it is treated as an Associative Array. Generally, the ValueObject that is passed back to a PHP function is treated as $valueObject["lastName"]. But we were pretty keen on using the generated code, which actually uses the syntax $valueObject->getLastName(). And this of course, failed, because the $valueObject being passed to PHP from Flex was not in fact being mapped back to the right VO, or any php VO for that matter.
Turns out, after much googling, that only under certain conditions will AMFPHP successfully map an incoming Flex VO to the corresponding PHP VO. In the globals.php, found at the root of the AMFPHP folder structure, you'll find the following line:
$voPath = "services/vo/";//file: src/tutorials/Person.as
and then in php:
package tutorials{
[RemoteClass(alias="tutorials.Person")]
[Bindable]
public class Person {
public var firstName:String;
public var lastName:String;
public var phone:String;
public var email:String;
}}<?php
To test the mapping add the following function to a PersonService.php file:
//file: services/vo/tutorials/Person.php
class Person {
var $firstName;
var $lastName;
var $phone;
var $email;
// explicit actionscript package
var $_explicitType = "tutorials.Person";}
function formatLastName(){
$lastName = strtolower($lastName); }
?>//file: services/tutorials/PersonService.php
Only if the valueobject was mapped correctly would it be able to call the function on the PHP VO. In Flex just create a Person object, pass it in to the remoteobject call, and observe the formatting of the last name on the Person object that was returned.
function modify($person){
//this will call a function on person and return it
$person->formatLastName();
return $person;
}
|
My Blog Title
|