<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wild Web Works Blogs &#187; Mapping</title>
	<atom:link href="http://www.wildwebworks.com/blogs/tag/mapping/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wildwebworks.com/blogs</link>
	<description>Programming</description>
	<lastBuildDate>Wed, 15 Apr 2009 21:41:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Passing Dates between Postgres and Flex</title>
		<link>http://www.wildwebworks.com/blogs/2009/04/passing-dates-between-postgres-and-flex/</link>
		<comments>http://www.wildwebworks.com/blogs/2009/04/passing-dates-between-postgres-and-flex/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 21:41:49 +0000</pubDate>
		<dc:creator>spidee</dc:creator>
				<category><![CDATA[Adobe Flex - Actionscript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Datatype]]></category>
		<category><![CDATA[Mapping]]></category>
		<category><![CDATA[Php Function]]></category>
		<category><![CDATA[Postgres]]></category>

		<guid isPermaLink="false">http://www.wildwebworks.com/blogs/?p=63</guid>
		<description><![CDATA[In my opinion, ActionScript has the worst datatype for dates.  They may be a joy to man handle in Flex, but they are not very compatible with any other program.

Our Postgres VO (types) uses timestamps and Flex VO has dates.  Here&#8217;s what you need to do to make that work back and forth.
We [...]]]></description>
			<content:encoded><![CDATA[<p>In my opinion, ActionScript has the worst datatype for dates.  They may be a joy to man handle in Flex, but they are not very compatible with any other program.</p>
<p><span id="more-63"></span></p>
<p>Our Postgres VO (types) uses timestamps and Flex VO has dates.  Here&#8217;s what you need to do to make that work back and forth.</p>
<p>We have a handy dandy utility class in Flex.  Here&#8217;s the code -</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package utility
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Used by VO objects that have dates coming into program from PHP</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">formatters</span>.<span style="color: #006600;">DateFormatter</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AMFDateConverter
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> convert<span style="color: #66cc66;">&#40;</span>value:<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> newDate:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span>
			<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span>,<span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">14</span>,<span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">17</span>,<span style="color: #cc66cc;">19</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> newDate;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Here&#8217;s the VO mapping in Flex.  I&#8217;m including the Boolean Converter, just in case you missed that in a prior blog.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package valueObjects
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> utility.<span style="color: #006600;">BooleanConverter</span>;
	<span style="color: #0066CC;">import</span> utility.<span style="color: #006600;">AMFDateConverter</span>;
	<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#91;</span>RemoteClass<span style="color: #66cc66;">&#40;</span>alias=<span style="color: #ff0000;">&quot;valueObjects.PeopleVO&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PeopleVO
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> pk_id:uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> p_first_name:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> p_last_name:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _p_birthday:<span style="color: #0066CC;">Date</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _p_active:<span style="color: #0066CC;">Boolean</span>;
&nbsp;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> p_birthday<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Date</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> _p_birthday;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> p_birthday<span style="color: #66cc66;">&#40;</span>value:<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>value is <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
				_p_birthday = AMFDateConverter.<span style="color: #006600;">convert</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				_p_birthday = value as <span style="color: #0066CC;">Date</span>;	
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span> 
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> p_active<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> _p_active;
	<span style="color: #66cc66;">&#125;</span> 
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> p_active<span style="color: #66cc66;">&#40;</span>value:<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		_p_active = BooleanConverter.<span style="color: #006600;">convertBool</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So that takes care of your date coming in.  Once Flex has it, there are some great manipulation tools in Actionscript.  Now when you need to send it back to Postgres, you&#8217;ll get PHP to do the work.<br />
Here&#8217;s the PHP VO class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> PeopleVO
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$pk_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$p_first_name</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$p_last_name</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$p_birthday</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$p_active</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here&#8217;s the PHP function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">function</span> dateConversion<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'NULL'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$input</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here&#8217;s the PHP code used to send the data to Postgres.  Postgres happily accepts it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> savePeople<span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$db</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$record</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">p_active</span> <span style="color: #339933;">=</span> boolConversion<span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">p_active</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$record</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">p_birthday</span> <span style="color: #339933;">=</span> dateConversion<span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">p_birthday</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM people_save((
		'<span style="color: #006699; font-weight: bold;">$record-&gt;pk_id</span>',
		'<span style="color: #006699; font-weight: bold;">$record-&gt;p_first_name</span>',
		'<span style="color: #006699; font-weight: bold;">$record-&gt;p_last_name</span>',
		<span style="color: #006699; font-weight: bold;">$record-&gt;p_birthday</span>,
		'<span style="color: #006699; font-weight: bold;">$record-&gt;p_active</span>'))&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PeopleVO'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Please note that the single quotes have been removed ON PURPOSE from the p_birthday.  If you send a null date back it looks like this &#8216;NULL&#8217;.  Postgres say&#8217;s that&#8217;s a string and arfs.  If you remove the quotes it comes back as NULL and Postgres takes it just fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wildwebworks.com/blogs/2009/04/passing-dates-between-postgres-and-flex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
