<?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; Pgsql</title>
	<atom:link href="http://www.wildwebworks.com/blogs/tag/pgsql/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>Using Value Objects in Postgres and Flex</title>
		<link>http://www.wildwebworks.com/blogs/2009/04/using-value-objects-in-postgres-and-flex/</link>
		<comments>http://www.wildwebworks.com/blogs/2009/04/using-value-objects-in-postgres-and-flex/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 14:43:34 +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[Pgsql]]></category>
		<category><![CDATA[Postgres Php]]></category>
		<category><![CDATA[Vo]]></category>
		<category><![CDATA[zend AMF]]></category>

		<guid isPermaLink="false">http://www.wildwebworks.com/blogs/?p=42</guid>
		<description><![CDATA[Using VO records with the Postgres/PHP/Flex trio is the way to go.  It guarantees that your objects come over in the correct format in direct correlation to what you have going on in all three programs.  ZendAMF really likes VOs, too.

There is one HUGE THING TO REMEMBER IN FLEX &#8211; You must instantiate [...]]]></description>
			<content:encoded><![CDATA[<p>Using VO records with the Postgres/PHP/Flex trio is the way to go.  It guarantees that your objects come over in the correct format in direct correlation to what you have going on in all three programs.  ZendAMF really likes VOs, too.</p>
<p><span id="more-42"></span></p>
<p>There is one HUGE THING TO REMEMBER IN FLEX &#8211; You must instantiate a VO to use it or refer to it on the page.  Sometimes you&#8217;ll have to just create a new instance that never goes anywhere or does anything.  Otherwise you&#8217;ll have problems that are really hard to understand.</p>
<p>Here&#8217;s a VO class in ActionScript</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;">AMFDateConverter</span>;
	<span style="color: #0066CC;">import</span> utility.<span style="color: #006600;">BooleanConverter</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.GLTransVO&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> GLTransVO
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> gl_trans_id:uint;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _trans_date:<span style="color: #0066CC;">Date</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> trans_amt:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> trans_note:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _trans_void:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> trans_desc:uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> trans_num:uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> trans_id:uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> gl_id:uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ref_id:uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> applied_amt:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> bal_amt:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> dr_amt:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> cr_amt:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> detail_amt:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ledger_bal:<span style="color: #0066CC;">Number</span>;
&nbsp;
 		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> trans_date<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> _trans_date;
		<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> trans_date<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>
				_trans_date = 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>
				_trans_date = 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> trans_void<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> _trans_void;
		<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> trans_void<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>
			_trans_void = 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>I chose this VO because it has both the boolean and date converter on it that we&#8217;ve provided in prior blogs.  Also, it has special calculated fields that don&#8217;t get saved in Postgres.  They do get passed in and Postgres uses the data in pl/pgsql functions.</p>
<p>This is one important feature of using VO records.  You have to anticipate what you might need to save or pass through the life of the record from Postgres to PHP to Flex and back again.  Rarely will your VO actually match the fields of any single table.  You could have table joins and data manipulation in Postgres to get that final VO record.  As long as you&#8217;re consistent, this works very well.</p>
<p>Here&#8217;s the PHP VO -</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> GLTransVO<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$gl_trans_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_date</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_amt</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_note</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_void</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_desc</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_num</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$trans_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$gl_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$ref_id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$applied_amt</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$bal_amt</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$dr_amt</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cr_amt</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$detail_amt</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$ledger_bal</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>This is very easy to build in PHP.  Just make sure it matches!</p>
<p>We actually build Types in Postgres that correspond to the VO.  It&#8217;s yet another way to make sure that data types stay true throughout the process.  It also REALLY, REALLY cuts down on the code to call a pl/pgsql function.  Normally you have to specify each field of the return record.  If you use Types, you just specify the type.</p>
<p>Here&#8217;s the code to create a Postgres Type -</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">TYPE</span> gl_trans_vo <span style="color: #00F;">AS</span>
   <span style="color: #00F;">&#40;</span>gl_trans_id <span style="color: #00F;">INTEGER</span><span style="color: #00F;">,</span>
    trans_date <span style="color: #00F;">TIMESTAMP</span> without <span style="color: #00F;">TIME</span> <span style="color: #00F;">ZONE</span><span style="color: #00F;">,</span>
    trans_amt numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
    trans_note text<span style="color: #00F;">,</span>
    trans_void <span style="color: #00F;">BOOLEAN</span><span style="color: #00F;">,</span>
    trans_desc <span style="color: #00F;">INTEGER</span><span style="color: #00F;">,</span>
    trans_num <span style="color: #00F;">INTEGER</span><span style="color: #00F;">,</span>
    trans_id <span style="color: #00F;">INTEGER</span><span style="color: #00F;">,</span>
    gl_id <span style="color: #00F;">INTEGER</span><span style="color: #00F;">,</span>
    ref_id <span style="color: #00F;">INTEGER</span><span style="color: #00F;">,</span>
    applied_amt numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
    bal_amt numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
    dr_amt numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
    cr_amt numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
    detail_amt numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
    ledger_bal numeric<span style="color: #00F;">&#40;</span><span style="color: #800;">10</span><span style="color: #00F;">,</span><span style="color: #800;">2</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span></pre></div></div>

<p>Here&#8217;s the beginning and the end of a Postgres function using the type.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">FUNCTION</span> gl_trans<span style="color: #00F;">&#40;</span>g_id <span style="color: #00F;">INTEGER</span><span style="color: #00F;">&#41;</span>
  RETURNS SETOF gl_trans_vo <span style="color: #00F;">AS</span>
$BODY$
<span style="color: #00F;">DECLARE</span>
gl_trans_list gl_trans_vo<span style="color: #00F;">;</span>
 <span style="color: #080; font-style: italic;">---------skipping code</span>
 <span style="color: #00F;">RETURN</span> NEXT gl_trans_list<span style="color: #00F;">;</span>
 <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span><span style="color: #00F;">;</span>
$BODY$</pre></div></div>

<p>This is called from PHP and passed to Flex.  Here&#8217;s the code for PHP</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> getGLTrans<span style="color: #009900;">&#40;</span><span style="color: #000088;">$g_id</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>
&nbsp;
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM gl_trans(<span style="color: #006699; font-weight: bold;">$g_id</span>) ORDER BY trans_date&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;">fetchAllObjects</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GLTransVO'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When you use debugging in Flex, you can actually see your VO with it&#8217;s proper name.  Here&#8217;s a screen shot of that -<br />
<img class="size-full wp-image-45 aligncenter" title="Using Value Objects" src="http://www.wildwebworks.com/blogs/wp-content/uploads/2009/04/4-13-2009-4-50-48-pm.png" alt="Using Value Objects" width="461" height="455" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wildwebworks.com/blogs/2009/04/using-value-objects-in-postgres-and-flex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
