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!