After my first article on WebOrb and dates in PHP I ran into another problem when sending dates from Flex to a PHP class through WebORB. This time it was a date that was sent as a property of a Value Object and not a parameter of the method. I finally decided it was time to figure this out once and for all.
I started by trying to google the problem but really didn’t find anything new. Other people have had the same issues and their solutions are pretty similar to the solutions I have used in the past. So I turned to digging through the WebORB source code. To make a long story short what I found is that dates are serialized into a class called ORBDateTime when they pass through WebORB. They only get serialized to DateTime if they are types as such. (ya, I know PHP is a dynamic language and data typing doesn’t really apply). If you want to see how this is done look in the weborb/Weborb/Reader directory at the DateType.php class.
So now knowing this it was easy to fix the problem. I simply now check what the class type is of my date objects using ‘get_class’ and if they are ‘ORBDateTime’ I do some conversions to make them compatible.
if(get_class($estimate->createdOn) == "ORBDateTime")
{
$createdOn = new DateTime(date("Y-m-d\TH:i:s\Z", $estimate->createdOn->getTotalMs() / 1000));
}
else
{
$createdOn = $estimate->createdOn;
}
// format it for MySQL
$createdOn->format("Y-m-d H:i:s");
In the above code first we check if the type of the date object is in fact ‘ORBDateTime’ and if so we convert it to a PHP DateTime object. (the conversion code is copied from the WebOrb DateType class). If this date is being inserted into a MySQL database then we need to format it again to be compatible with the MySQL DateTime field. The conditional is simplified and I do not check if the date is actually a DateTime object. The reason for this is I am pretty certain it is because there are only two ways this method gets called – from Flex when we know it will be ORBDateTime and from my test class where I have constructed a DateTime object.
This works like a charm and I hope this finally solves the problem of dates, Flex and WebOrb for PHP.