Transfer a Date from PHP to Flex using amfPHP 2.1

Problem

You wish to transfer a Date from your PHP code to Flex using amfPHP. How do you do that?

Solution

You use the Amfphp_Core_Amf_Types_Date object in your PHP code which amfPHP will convert to a Flex date.

VO object (named DataLine). This will be in the VO directory of your AMF installation, and I'm only going to be using the some_date property in this example.

class DataLine
{
public $some_id;   
public $some_int; 
public $some_char;
public $some_date;
};

Service Object (named Bravo). Here I have defined a method get_date which I'll connect to via amfPHP.

require_once 'VO/DataLine.php';
 
class Bravo
{
 
public function __construct()
{
 
}
 
public function get_date()
{
//lets create a dataline object, and populate it with some data...
$return_data = new DataLine();
 
//ANZAC Day Dawn Service
$date = new DateTime('2013-04-25 04:10:00');
 
//Amfphp_Core_Amf_Types_Date expects a UNIX timestamp in milliseconds.
$unix_date = $date->format('U');
 
//PHP can return UNIX timestamp in seconds, therefore we must multiple by
//1000 to pass in the correct value otherwise we will get a very strange date.
$return_data->some_date = new Amfphp_Core_Amf_Types_Date($unix_date * 1000);
 
return $return_data;
}
 
};

Flex Code

This is only an abridged version to demonstrate the amfPHP date interaction. If you need a more comprehensive example, see my RPC tutorial.

The RemoteClass directive points to the name of the PHP object that your Flex code connects with. With amfPHP the name of the classes in PHP/Flex can differ (as I have done in this example), however ZendAMF (which has been silently dropped from the Zend Framework) requires that the names be the same between Flex and PHP. Please note the comments in the code if you should omit the RemoteClass directive.

DataLineFlex Value Object.

package VO
{

/* must have the remoteClass directive otherwise the line:
... event.result as DataLineFlex;
will result in an error.
*/
[RemoteClass(alias="DataLine")]
[Bindable]
public class DataLineFlex
{
public var some_id : int;
private var _some_int : int;
public var some_char : String;
public var some_date : Date;

public function DataLineFlex()
{

}

}
}

Main MXML file. I have had to remove a forward slash from the fx value in the Application tag so that the code would display properly. Keep this in mind if you are going to copy and paste. I have also added some comments about the RemoteObject and what some of the attributes mean.


xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">


import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;

private var myDataLine : DataLineFlex;

protected function button2_clickHandler(event:MouseEvent):void
{
Bravo.get_date();
}

protected function get_date_resultHandler(event:ResultEvent):void
{
this.myDataLine = event.result as DataLineFlex;
Alert.show("The message is: " + myDataLine.some_date.toString(),
"Bravo PHP service method");
}

]]>

source="Bravo"
destination="amfphp">
result="get_date_resultHandler(event)" />


When I click on the Test Date button I get the following Alert box showing the value retrieved from the RemoteObject call to the get_date method of the Bravo object.

Tags: 

Comments

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.