Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
Flex + AMFPHP: Mapping Value Objects
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:
-
[RemoteClass(alias="Book")]
and
-
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
$_explicitTypevariable 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 :
-
$voPath = $_SERVER["DOCUMENT_ROOT"]."/path/to/vo/directory";
Here's a more flushed out code sample:
-
package com.website.vos
-
{
-
[RemoteClass(alias="Book")]
-
[Bindable]
-
public class Book
-
{
-
-
public var title:String;
-
public var author:String;
-
-
public function Book()
-
{
-
}
-
-
-
}
-
}
-
<?php
-
-
class Book {
-
-
var $_explicitType="Book";
-
var $title;
-
var $author;
-
-
-
}
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:
-
$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:
-
[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:
-
var $_explicitType="com.website.vos.Book";
| Print article | This entry was posted by rich on August 2, 2008 at 5:09 pm, and is filed under flash, php. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 4 years ago
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
about 4 years ago
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.
about 4 years ago
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
about 4 years ago
Could you perhaps post a little more code for context.
Thanks
about 4 years ago
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).
about 4 years ago
thanks a lot. Very helpful!
about 4 years ago
Yes! Thank you for confirming the relationship between the Remote alias and the $_explicitType.
I am wondering if it's worth creating complex objects (as opposed to 1 - 1 table representations/VO's) other than nested Objects.
BTW: does it have to be [Bindable] for all this to work ?
Cheers
about 4 years ago
thanx...solved my problem !!!
about 4 years ago
could someone post a fully working example project to study please.
cheers dl
about 4 years ago
Is it possible to map a custom MXML component to a php class? If so, where do I put the [RemoteClass(alias="SomeClass")] tag?
Also, do all of the data members in my MXML component need to also appear in the php class?
I'd really appreciate some help.
Thanks
about 4 years ago
If you're talking about a UI component, then technically you might be able to do it, but overall you wouldn't want to. Instead, you'd just store the data you'd want to save into a separate value object and send that back and forth.
about 4 years ago
Good point. Thanks for the help.
about 3 years ago
Hi,
Is there any tools to generate the VO class for Flex, by specifying the location of PHP VO.
The Whole point:
Generation VO Classes for all PHP VO class in one stretch.
about 3 years ago
Hi,
Sorry, i have not read the post properly before. Rich , you have mentioned that you use Propel to generate the VO classes, can you tell the steps to generate it please.
about 3 years ago
Excelent!
Its great and useful..
Thanks for this
about 3 years ago
Thank you so much my friend! Really helpful!! Especially for the part that explain where to set the $voPath.
about 3 years ago
"Is it possible to map a custom MXML component to a php class? If so, where do I put the [RemoteClass(alias="SomeClass")] tag?
Also, do all of the data members in my MXML component need to also appear in the php class?
I'd really appreciate some help.
Thanks"
Use the tag in Flex 3 or in flex 4.
about 3 years ago
Thanks, that was useful while trying to figure out the mapping for a project at work. I'm quite amazed it's this hard though with php, gotta love Python.
about 3 years ago
I had an issue with processing an array of value objects : they got returned as plain objects.
Solution : import the value object class so flex knows about it
about 3 years ago
yeah, flex always has to know that a class actually exists in order to map it
Otherwise, just like any other class that doesn't appear in your app, the compiler will assume you're not using it at all, and not compile it into the swf.
about 3 years ago
Hi guys,
Is someone using AMFPHP, propel, VO mapping ??
Could someone upload a sample of AmfPHP service??
I need to understand how to map the propel class (with $_explicittype) with the "vo return" in the amfphp service.
How can I "fetch_object" to a propel class ???
I have the propel object into a amfphp service method but I don't find how to pass back to flash /flex
Thanks in advance
Luca
PS: sorry for my english.
about 3 years ago
Hi
I have a small app that retrieves data from 2 different table of my mysql db and I have written vo code for both of them (client and server). For some reason, the first class is properly recognised, but the second is returned as a plain object.
Any clue why this is happening?
Thanks a lot
Stix
about 3 years ago
Make sure that the mapping is correct, and make sure that the second class is actually available before using the first one.
about 3 years ago
Thank you a lot !!
Very useFull and 'Clair' in french ^^
good job
about 3 years ago
Thank you very much, you are illuminate me
great example..
about 2 years ago
I feel so much hppiaer now I understand all this. Thanks!
about 1 year ago
hmmm... I cannot see my comment.
here it is again!:
I thought there was supposed to be something like a "$voPath" variable in which we set the path inside which amfPHP will look to match the incoming class from Flex applications... however, I cannot find this variable in AmfPHP 2.0... how amfphp will know where to find those vo classes?
thanks
about 1 year ago
I haven't looked at amfphp 2.0 yet, but from what I understand, it's a pretty massive rewrite and the entire setup would be different than previous versions, so this probably wouldn't apply.