Wild Web Works Inc.

April 2, 2009

Actionscript vs Postgres – Boolean Values

Actionscript does not send boolean values that Postgres can take. You need to work a little magic on the way over.

For VO records coming in from Postgres via PHP we use the following class.

package utility
{
	public class BooleanConverter
	{
		public static function convertBool(pgBool:String):Boolean{
			var newBool:Boolean = new Boolean;
			if(pgBool == "t" || pgBool == "T" || pgBool == "1" || pgBool == "true")
			{
				newBool = true;
			}else{
				newBool = false;
			}
 
			return newBool;
		}
	}
}

Then we use getter/setter on the VO calling the class.

	[Bindable]
	[RemoteClass(alias="valueObjects.OwnerVO")]
 
	public class OwnerVO
	{
		public var pk_id:uint;
		public var owner_company:String;
		private var _owner_active:Boolean;
 
		public function get owner_active():Boolean
		{
			return _owner_active;
		} 
		public function set owner_active(value:*):void
		{
			_owner_active = BooleanConverter.convertBool(value);
		}
	}
}

You have to convert the other direction, too. Saving from Actionscript to Postgres requires PHP to step in and perform some magic. Here’s the PHP function, which we have on ‘conversions.php’.

	function boolConversion($input){
 
		switch($input){
			case 't':
				$output = 1;
				break;
			case '1':
				$output = 1;
				break;
			case 'f':
				$output = 0;
				break;
			case '':
				$output = 0;
				break;
		}
		return $output;
	}

Here’s the PHP page receiving the VO from Flex and sending it over to Postgres.

require_once("conversions.php");
 
class Owners
{
	public function saveOwner($record)
	{
		global $db;
 
		$record->owner_active = boolConversion($record->owner_active);
 
		$sql = "SELECT * FROM owner_save((
		'$record->pk_id',
		'$record->owner_company',
		'$record->owner_active'))";
 
		$db->query($sql);
		return $db->fetchObject('OwnerVO');
	}
}

Once this is set up, it works really well. Enjoy!

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

2 Comments »

  1. create function bool_to_int(boolean) returns int as ‘
    select case when $1 then 1 else 0 end
    ‘ language sql strict immutable;

    create operator @ (
    leftarg = boolean,
    procedure = bool_to_int
    );

    create table test (b boolean);
    insert into test values (true);
    insert into test values (false);
    insert into test values (null);

    select b,b@ from test;

    create view testv (b) as
    select b@ from test;

    select * from testv;

    Comment by PaweÅ‚ — August 4, 2009 @ 12:52 am

  2. Hello,

    Thanks for explaining this problem.

    I’m using MySQL with PDO and I had to use your AS3 class BooleanConverter to get the correct values. But I don’t need to convert back from AS3 to PHP.

    Now it looks like I’ve to do the same kind of class to convert MySQL datetime into AS3 Date.

    Comment by Olivier — August 21, 2009 @ 3:04 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment