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.
Comments
Newbie Question on Code location
Hi Andrew, thanks for talking about this, I as-well had the same problem, but your smarter then me. been looking for the answer/solution for a while. can you help....
My question is: your Code would it go into, DateType.php class it self or somewhere else? DateReader.php..maybe???
Or would it go into Generated services, made by weborb.
Say if i was using test drive generated code, would it apply here..
I'm not positive where to put it.
Any suggestion.
Regards
Nathan
Code location reply
Hi Nathan,
I just put it in my service class for now. I don't think editing the WebOrb source code is a good idea. I am working on another project that has the same issue so I am going to write a utility class that handles this.
amf
Hey Andrew, your solution looks like genius, if only it worked for AMF! I'm using Zend AMF with Flex and I'm having similar issues.
I have my dates stored in mysql as datetime values. I use php to select records and convert the timestamps to unix time with the unix_timestamp() function in the sql query.
I send the unix date (* 1000 - flex does these in milliseconds instead of seconds) back to flex as a property of a value object and then I use your AMFDateConverter to alter the Number to a Date class. This allows me to render my dates in my datagrid but I'm stumped then as to how I send the dates back to the server when they get updated by users. I've spent over a week searching and asking questions on forums to no avail. If you have any idea how I can achieve this pleeeeeaaaaasssssee help me out, think of it as your good deed for the day, I'll pay it forward
;o)
amf reply
Not sure what you are asking. Does my reply to the comment below answer you question?
zend
hey andrew, I left a comment asking for help on your other thread about dates. I'm using Zend AMF, I notice you are using weborb instead, I don't suppose you happen to know of a similar solution in Zend AMF??
;o)
zend reply
I am not 100% sure but after looking at the Zend_AMF source it looks like Flex dates get converted to Zend_Date objects. You can just update the code I provided to check for this instead of OrbDateTime. If you want to confirm what type Zend converts your dates to you can simply use the get_class php method to return the class name to Flex and trace it out. Like this
public function dateTest($date)
{
return get_class($date);
}
Weborb + php + dates
Hi ANDREW. Thanks for helping us - the rookies - on this issue.
I don't understand "where" neither "in which" php file to write your suggested call...please, could you be more explicit?
Appreciated,
Carlos
See my reply to the first
See my reply to the first comment