Skip to Content »

FlashApe » Flex + AMFPHP: Mapping Value Objects

 Flex + AMFPHP: Mapping Value Objects

  • August 2nd, 2008
  • 5:09 pm

Update: Added code samples to end of post.

One of the issues I see a lot of with people using AMFPHP is how to properly map VO classes between Flex and php. It used to stress me out too, until I realized how easy it really is.

First thing: Don't worry about the package paths in the RemoteClass metadata tag and $_explicitType variables in your php classes. You don't need them.

Second thing: Just use the name of the class you are mapping in for the values RemoteClass("alias="") and $_explicitType="". For example, if you wanted to map a value object named 'Book' between flex and php, just use:

Actionscript:
  1. [RemoteClass(alias="Book")]

and

PHP:
  1. var $_explicitType="Book";

Like i already mentioned, you don't need the package names. The reasons behind this are:

  • When sending from php to Flex, Flex will match the $_explicitType variable to the Remote class alias. It's just an id to match...it doesn't even have to be the class name...it can be jibberrish, as long as both strings match. (This won't work the other way though...see next item)
  • When sending from Flex to php, AMFPHP will try to load the class with the name specified by the RemoteObject alias value. It simply looks in a specified directory to try and load the class...if it can't find it it's parsed as an array.

So, how does AMFPHP know where to look for the vo classes? Easy...you tell it exactly which directory to look in by setting the $voPath variable in globals.php. I usually do something along the lines of :

PHP:
  1. $voPath = $_SERVER["DOCUMENT_ROOT"]."/path/to/vo/directory";

Here's a more flushed out code sample:

Actionscript:
  1. package com.website.vos
  2. {
  3.     [RemoteClass(alias="Book")]
  4.     [Bindable]
  5.     public class Book
  6.     {
  7.        
  8.         public var title:String;
  9.         public var author:String;
  10.        
  11.         public function Book()
  12.         {
  13.         }
  14.        
  15.  
  16.     }
  17. }

PHP:
  1. <?php
  2.  
  3. class Book {
  4.  
  5.     var $_explicitType="Book";
  6.     var $title;
  7.     var $author;
  8.  
  9.  
  10. }

Say for example the Book.php class file is sitting on the server at webroot/php/classes/vos/Book.php. In order to tell AMFPHP where to look for the Book.php file, you open up globals.php in your AMFPHP installation, and set the $voPath variable to point to the 'vos' folder:

PHP:
  1. $voPath = $_SERVER["DOCUMENT_ROOT"]."/php/classes/vos";

Now when you send a Book object from Flex to php, AMFPHP will look in the $voPath (which we just set to the 'vos' folder) for the Book.php file.

Note that the $voPath variable is the starting point of where AMFPHP will start to look. If you specify a full package path for the RemoteClass alias, such as:

Actionscript:
  1. [RemoteClass(alias="com.website.vos.Book")]

then all the dots in the string are converted to slashes, and the full path is used. To continue with the example above, we have already set $voPath to point to the 'vos' folder, and the RemoteClass alias has been set to "com.website.vos.Book". AMFPHP converts the alias to "com/website/vos/Book" and will attempt to locate the file $voPath/com/website/vos/Book.php. In addition, if you're mapping this object from php back top Flex, remember the $_explicitType variable still must match the string for the RemoteClass alias, so you'd need to set that to:

PHP:
  1. var $_explicitType="com.website.vos.Book";

6 People had this to say...

Gravatar

Hi,
I've published a very simple webapp which generates PHP and ActionScript ValueObjects code to use into AMFPHP projects.

After generation, the code may need to be customized but it's a good starting point I frequently use.

You can find it here:
http://www.alessandroronchi.com/amfphpadv/

Best regards, Alessandro

Gravatar
  • rich
  • August 3rd, 2008
  • 10:32 am

That's a nice app alessandro. I'm actually using Propel to auto-generate all of my classes, and I've modified their source to output what I need to work with Flex.

Gravatar
  • Alan
  • August 3rd, 2008
  • 10:56 am

WIth

[RemoteClass(alias="Book")]

Is this metatag decaled at the class level of your VO?

package Book
{
[RemoteClass(alias="Book")]

public class BookVO
{

}

}

class Book
{
var $firstName;
var $lastName;
var $phoneNumber;
}

IS this correct? THis whole class mapping thing is still cloudy for me

Gravatar
  • Alan
  • August 3rd, 2008
  • 10:58 am

Could you perhaps post a little more code for context.

Thanks

Gravatar
  • rich
  • August 4th, 2008
  • 8:16 am

you've pretty much got it. The RemoteClass metadata tag is defined right above your class definiton, check the livedocs here and here.

You just need to add the $_explicitType var to the php class, and make it match whatever string you entered for the "alias" value in the RemoteClass tag, which in your case was "Book".

(Note that in the livedocs they mention a few times about matching the class path in the RemoteClass tag...I'm assuming that's Java-specific. As noted, you just need to match the two strings between the RemoteClass "alias" and the $_explicitType php var).

Gravatar
  • Ved
  • October 3rd, 2008
  • 6:34 am

thanks a lot. Very helpful!

Want your say?

* Required fields. Your e-mail address will not be published on this site


You can use the following XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>