php

php topics

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:

PHP:
  1. if($typeIdentifier == "flex.messaging.io.ArrayCollection")
  2. {
  3.         return new ArrayCollection();
  4. }

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:

PHP:
  1. if( $classDefinition['externalizable'] )
  2. {
  3.     if($type == 'flex.messaging.io.ArrayCollection')
  4.     {
  5.         $obj = $this->readAmf3Data(true);
  6.     }

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:

PHP:
  1. function readAmf3Data($isArrayCollection = false)
  2. {
  3.     $type = $this->readByte();
  4.    
  5.     switch($type)
  6.     {
  7.         case 0x00 : return null; //undefined
  8.         case 0x01 : return null; //null
  9.         case 0x02 : return false; //boolean false
  10.         case 0x03 : return true//boolean true
  11.         case 0x04 : return $this->readAmf3Int();
  12.         case 0x05 : return $this->readDouble();
  13.         case 0x06 : return $this->readAmf3String();
  14.         case 0x07 : return $this->readAmf3XmlString();
  15.         case 0x08 : return $this->readAmf3Date();
  16.         case 0x09 : return $this->readAmf3Array($isArrayCollection);
  17.         case 0x0A : return $this->readAmf3Object();
  18.         case 0x0B : return $this->readAmf3XmlString();
  19.         case 0x0C : return $this->readAmf3ByteArray();
  20.         default: trigger_error("undefined Amf3 type encountered: " . $type, E_USER_ERROR);
  21.     }
  22. }

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:

PHP:
  1. function readAmf3Array($isArrayCollection = false)
  2. {
  3.     $handle = $this->readAmf3Int();
  4.     $inline = (($handle & 1)  != 0 ); $handle = $handle>> 1;
  5.  
  6.     if( $inline )
  7.     {
  8.         $hashtable = $isArrayCollection ? new ArrayCollection() : array();
  9.        
  10.         $this->storedObjects[] = & $hashtable;
  11.         $key = $this->readAmf3String();
  12.         while( $key != "" )
  13.         {
  14.             $value = $this->readAmf3Data();
  15.             $hashtable[$key] = $value;
  16.             $key = $this->readAmf3String();
  17.         }
  18.  
  19.         for($i = 0; $i <$handle; $i++)
  20.         {
  21.             //Grab the type for each element.
  22.             $value = $this->readAmf3Data();
  23.             $hashtable[$i] = $value;
  24.         }
  25.         return $hashtable;
  26.     }
  27.     else
  28.     {
  29.         return $this->storedObjects[$handle];
  30.     }
  31. }

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.

Frameworks 2.0

Subititled: "The Rise of 'Burger King' frameworks....'have it your way'".

I am loving the new trend I'm seeing in a lot of the frameworks (both client- and server-side) I've been checking out lately. What I'm seeing is a move away from the monolithic, "do it my way or suffer trying to work around it" approach, towards more architectures that provide a more generalized, "less is more" approach, even to the point that some of the functionality and/or features of the "do-it-all" frameworks are being broken out into smaller, specialized bits that allow the developer to pick and choose between whichever approach best suits their style.

To me, the main benefit of the new trend is readily apparent: just as different individuals would write the same story different ways, the same goes for writing code. For one framework to dictate too much influence over too many aspects of an application, or even worse to make it difficult to extend or adapt to suit your needs, provides too much lock-in and and can hinder more than it's trying to help.

On the flash/flex side, frameworks like RobotLegs , Cairngorm 3, Gaia, and even libraries like CasaLib provide example of this (ok, Gaia probably goes a little further as far as initial structure, but once you start developing it stays out of your way). On the backend, for php you have things such as Konstrukt, a "URI-to-controller-mapping" (i.e., REST) framework which handles "routing based on logic rather than rules", and the Outlet and phpDataMapper ORM frameworks.

One thing about the above mentioned frameworks is that most, if not all, can be used in conjunction with each other (like so). If you don't like the way one part does it's job, you can more than likey swap out that part with a similar one without affecting the other parts of an application, provided you we good with encapsulation and keeping things pretty cleanly separated. This is not an insignificant side effect of the "less-is-more" approach, it's the central theme. It's having it your way.

Propel DATETIME errors

If you use the PHP ORM framework Propel, you may have hit the error "DateTime::__construct(): Failed to parse time string (CURRENT_TIMESTAMP)" when generating your php classes if a column in your table schema uses DATETIME. Im previous version it seemed like the generator would ignore the error and continue to generate the rest of the classes, but I recently updated my installation and now it breaks it.

I did some searching at first and saw someone saying that it was because the date.timezone setting for php was not set. So I added date.timezone=UTC to php.ini, and changed the PHP_COMMAND in the phing script to include the ini file, since running php from the command line does not automatically include it:
PHP_COMMAND="/Applications/MAMP/bin/php5/bin/php -c /Applications/MAMP/conf/php5/php.ini"

None of that worked, however, and after digging around a bit more I found there is a new undocumented attribute you can use in your schema.xml file called defaultExpr, which will insert the result of the expression directly into the database. So I just set defaultExpr="now()" and everything was good to go.

Nice Yum Repository

I'd love to hear if anyone has some other recommendations, but if you're using RHEL/CentOS and you want to upgrade your php/mysql installation using Yum, you should check out the utterramblings yum repository. It has the latest versions of php, mysql, and a few others. I upgraded today and it was instant and painless (a rarity).

Cool MySQL increment/decrement value tip

I needed to adjust a value in a column in a database table by 1, for all rows. I found out you can do it easily by doing this:

PHP:
  1. $sql = "UPDATE images SET displayIndex=displayIndex +1 WHERE gallery_id=1";

Turning that into a function we can specify if we want to increment or decrement the value pretty easily. You can also provide a starting point for the adjustment (so if you delete the item with displayIndex=15, you only want to decrement the displayIndexes greater than 15 by 1) and specify a different value for the WHERE clause:

PHP:
  1. public function updateDisplayIndexes($galleryID, $startIndex, $add = true){
  2.    
  3.     $operator = ($add) ? '+' : '-';
  4.  
  5.     $sql = "UPDATE images SET displayIndex=displayIndex".$operator."1 WHERE gallery_id=$galleryID AND displayIndex> $startIndex"
  6.     $result = mysql_query ($sql);
  7. }

There's a lot more you can do with it but it's been a pretty good starting point.

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:

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";

Sending Negative Integers through AMFPHP

There is a weird bug in AMFPHP regarding sending negative integers. If you try and send a number such as -87, it shows up as 4294967209 in php. I did some digging and found on the amfphp forums that it has to do with amfphp's readAmf3Int() method in AMFDeserialzer.php. Here is the updated function that was posted on the forum:

PHP:
  1. function readAmf3Int()
  2.     {
  3.         $res = 0;
  4.         $int = $this->readByte();
  5.        
  6.         if($int <128) {
  7.             return $int;
  8.         } else {
  9.             $int = ($int & 0x7f) <<7;
  10.            
  11.             $tmp = $this->readByte();
  12.            
  13.             if($tmp <128) {
  14.                 $int |= $tmp;
  15.             }else{
  16.                 $int = ($int | ($tmp & 0x7f)) <<7;
  17.                 $tmp = $this->readByte();
  18.                 if($tmp <128){
  19.                     $int |= $tmp;
  20.                 }else{
  21.                     $int = ($int | ($tmp & 0x7f)) <<8;
  22.                     $tmp = $this->readByte();
  23.                     $int |= $tmp;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         $mask = 1<<28;
  29.         $res = -($int & $mask) | $int;
  30.  
  31.         return $res;
  32.     }

AMFEXT doesn’t handle incoming arraycollections

Ran into an issue today with AMFPHP trying to send an ArrayCollection...it kept choking when trying to decode the collection. I knew this was already possible, but forgot I had installed the php extension AMFEXT, which handles the encoding/decoding for amfphp when it is installed. Turns out that it doesn't like incoming arraycollections. Disabling the extension fixed my problem, but I would sure like find a way to get them working together.

IMagick – ImageMagick extension for PHP

If you're spending a decent amount of time manipulating images in php using GD or command-line ImageMagick, you should check out the Imagick php extension. Imagick provides an object-oriented interface to the ImageMagick API.

As far as I can tell, this extension was left for dead a while ago but revitalized over the last year or so and is now rockin pretty hard. The developer is pretty responsive on the Imagick board at the ImageMagick forums as well, and he's got lots of nice code tidbits on his own blog.

There is also the MagickWand For PHP extension, but I found the syntax pretty odd to deal with, it probably relates more to the way GD works than the OO style of Imagick.

Client.Error.DeliveryInDoubt

Finally getting back into using Flex again, and was trying out the Remote Object tutorial here. Everything went fine for the Hello World tutorial, but I kept getting 'Client.Error.DeliveryInDoubt' messages from the PersonService. I read a couple of places that there had been issues with charset handling in amfphp, so spent a bunch of time with that, but still no luck. Then I remembered that I created the php file for HelloWorld in TextMate, and create the 2 php files for the PersonService in Flex Builder. Sure enough, after recreating the php files in TextMate everything worked. I'm not sure if it was an encoding issue, since it seems like both TextMate and FlexBuilder default to utf-8. I did notice that in FB, the preference for 'New Text File Line Delimiter' (under General/Workspace) was set to default, so I changed it to Unix to line up with TextMate.

Then I went out and installed PDT to set up a PHP perspective within FB.