Wild Web Works Inc.

April 14, 2009

Using Value Objects in Postgres and Flex

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 – You must instantiate a VO to use it or refer to it on the page. Sometimes you’ll have to just create a new instance that never goes anywhere or does anything. Otherwise you’ll have problems that are really hard to understand.

Here’s a VO class in ActionScript

package valueObjects
{
	import utility.AMFDateConverter;
	import utility.BooleanConverter;
	[Bindable]
	[RemoteClass(alias="valueObjects.GLTransVO")]
 
	public class GLTransVO
	{
		public var gl_trans_id:uint;
		private var _trans_date:Date;
		public var trans_amt:Number;
		public var trans_note:String;
		private var _trans_void:Boolean;
		public var trans_desc:uint;
		public var trans_num:uint;
		public var trans_id:uint;
		public var gl_id:uint;
		public var ref_id:uint;
		public var applied_amt:Number;
		public var bal_amt:Number;
		public var dr_amt:Number;
		public var cr_amt:Number;
		public var detail_amt:Number;
		public var ledger_bal:Number;
 
 		public function get trans_date():Date
 		{
			return _trans_date;
		}
		public function set trans_date(value:*):void
		{
			if(value is String)
			{
				_trans_date = AMFDateConverter.convert(value);
			}else{
				_trans_date = value as Date;
			}
 
		}
 
		public function get trans_void():Boolean
		{
			return _trans_void;
		}
		public function set trans_void(value:*):void
		{
			_trans_void = BooleanConverter.convertBool(value);
		}
	}
}

I chose this VO because it has both the boolean and date converter on it that we’ve provided in prior blogs. Also, it has special calculated fields that don’t get saved in Postgres. They do get passed in and Postgres uses the data in pl/pgsql functions.

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’re consistent, this works very well.

Here’s the PHP VO -

<?php
 
class GLTransVO{
 
	public $gl_trans_id;
	public $trans_date;
	public $trans_amt;
	public $trans_note;
	public $trans_void;
	public $trans_desc;
	public $trans_num;
	public $trans_id;
	public $gl_id;
	public $ref_id;
	public $applied_amt;
	public $bal_amt;
	public $dr_amt;
	public $cr_amt;
	public $detail_amt;
	public $ledger_bal;
}
?>

This is very easy to build in PHP. Just make sure it matches!

We actually build Types in Postgres that correspond to the VO. It’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.

Here’s the code to create a Postgres Type -

CREATE TYPE gl_trans_vo AS
   (gl_trans_id INTEGER,
    trans_date TIMESTAMP without TIME ZONE,
    trans_amt numeric(10,2),
    trans_note text,
    trans_void BOOLEAN,
    trans_desc INTEGER,
    trans_num INTEGER,
    trans_id INTEGER,
    gl_id INTEGER,
    ref_id INTEGER,
    applied_amt numeric(10,2),
    bal_amt numeric(10,2),
    dr_amt numeric(10,2),
    cr_amt numeric(10,2),
    detail_amt numeric(10,2),
    ledger_bal numeric(10,2));

Here’s the beginning and the end of a Postgres function using the type.

CREATE OR REPLACE FUNCTION gl_trans(g_id INTEGER)
  RETURNS SETOF gl_trans_vo AS
$BODY$
DECLARE
gl_trans_list gl_trans_vo;
 ---------skipping code
 RETURN NEXT gl_trans_list;
 END LOOP;
END;
$BODY$

This is called from PHP and passed to Flex. Here’s the code for PHP

	public function getGLTrans($g_id)
	{
		global $db;
 
		$sql = "SELECT * FROM gl_trans($g_id) ORDER BY trans_date";
 
		$db->query($sql);
		return $db->fetchAllObjects('GLTransVO');
 
	}

When you use debugging in Flex, you can actually see your VO with it’s proper name. Here’s a screen shot of that -
Using Value Objects

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Furl
  • LinkedIn
  • Spurl
  • StumbleUpon

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment