DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jonnie has posted 12 posts at DZone. View Full User Profile

Full Service Class For Amfphp

09.21.2008
| 3596 views |
  • submit to reddit
        Here is a very helpful script that will allow you to create, update, delete, and read all data from your database through amfphp. When you receive the data in Flex you will have a success mapping of the value objects. Code is as follows:

<?php
/** *******************************************************************
 * @author  Jonnie Spratley
 * @internal
 ******************************************************************* */
class SnipprService
{
	//Specify our output temp directory
	var $output_dir = "screenshots";
	
	//Specify our output url 
	var $server_url = "http://localhost/snippr";
	
	//Specify our table name
	private $table = "snippets";
		
	public function SnipprService()
	{
		mysql_connect("localhost", "username", "password");
		mysql_select_db("snippets") ;	
	}


	private function mapRecordSet( $recordset )
	{
		require_once( "../vo/SnippetVO.php" );
		$list = array();
		
		while( $data = mysql_fetch_array( $recordset ) )
		{
			$vo = new SnippetVO();
			$vo->mapObject( $data );
			array_push( $list, $vo );
		}		
		return $list;	
	}	


	public function getSnippets()
	{
		//We must specify our vo, because we need to map correctly
		require_once( "../vo/SnippetVO.php" );
		
		$sql = mysql_query( "SELECT * FROM ". $this->table. "" );
		
		$result = array();

		while( $snip = mysql_fetch_array( $sql ) )
		{
			//Create a new snippet vo
			$snippet = new SnippetVO();
			$snippet->snippet_id 			= $snip[snippet_id];
			$snippet->snippet_title 		= $snip[snippet_title];
			$snippet->snippet_code 			= $snip[snippet_code];
			$snippet->snippet_type 			= $snip[snippet_type];
			$snippet->snippet_created 		= $snip[snippet_created];
			$snippet->snippet_user		 	= $snip[snippet_user];
			//Result is a snippet
			$result[] = $snippet;
		}
		//Print out the result
		return $result;
	}

	//This is used for returning the created or updated snippet for flex
	public function getOne( $id )
	{
		$rs = mysql_query( "SELECT * FROM ".$this->table." WHERE snippet_id = ".$id );
		//Map the recordset to our vo
		$list = $this->mapRecordSet( $rs );
		//Return our vo
		return $list[ 0 ];		
	}
	
	//Creates a new snippet
	public function saveSnippet( $snippet )
	{
		require_once( "../vo/SnippetVO.php" );
		//Check to see if the snippet has an id of 0
		if ( $snippet[snippet_id] == 0 ) 
		{
		$query = "INSERT INTO ".$this->table."
								( snippet_title,
								snippet_code,
								snippet_type,
								snippet_created,
								snippet_user )		
								VALUES (
								'".mysql_real_escape_string($snippet[snippet_title])."',
								'".mysql_real_escape_string($snippet[snippet_code])."',
								'".mysql_real_escape_string($snippet[snippet_type])."',
								'".mysql_real_escape_string($snippet[snippet_created])."',
								'".mysql_real_escape_string($snippet[snippet_user])."')";
		if( !mysql_query( $query ) ) 
		{		
			return false;
		}				
			return $this->getOne( mysql_insert_id() );
			
			} else {
					$id = $snippet[snippet_id];
			
					$query = "UPDATE ".$this->table." SET 
					snippet_title = '".mysql_real_escape_string($snippet[snippet_title])."',
					snippet_code = '".mysql_real_escape_string($snippet[snippet_code])."',
					snippet_type = '".mysql_real_escape_string($snippet[snippet_type])."',
					snippet_created = '".mysql_real_escape_string($snippet[snippet_created])."',
					snippet_user = '".mysql_real_escape_string($snippet[snippet_user])."'
			
					WHERE snippet_id =". $id;
				
					if( !mysql_query( $query ) )
					{		
						return false;		
					}				
					//Return the created snippet
					return $this->getOne( $id );				
				}				
	}

	public function removeSnippet($id)
	{
		$sql = mysql_query( "DELETE FROM ".$this->table." WHERE snippet_id = ".$id );
		
		if( !$sql )
		{
		//	trigger_error("Unable to delete Snippets", E_USER_ERROR);
			return "There was an error removing this snippet";
		}
		else return $id;		
	}      
      

	//Save image from the given bytearray and return the path of the saved image
	public function takeScreenshot( $byteArray, $filename )
	{
		if( !file_exists( $this->output_dir ) || !is_writeable( $this->output_dir ) )
			trigger_error ( "Please create a directory first with write access", E_USER_ERROR );

		$data = $byteArray->data;
		
		//Put the File in the Directory, and Rename it, what the User wanted the Name to be.
		file_put_contents( $this->output_dir . "/$filename", $data );
	   
	   return $this->server_url . $this->output_dir . "/$filename";
	}
}
?>

And here is the Value Object 


<?php
/** *******************************************************************
 * @author  Jonnie Spratley
 * @internal
 ******************************************************************* */
class SnippetVO 
{
	var $_explicitType="com.jonniespratley.snippr.vo.SnippetVO";
	
	var $snippet_id;
	var $snippet_title;
	var $snippet_code;
	var $snippet_type;
	var $snippet_created;
	var $snippet_user;
	
	public function SnippetVO(){}
	
	//Now map the database tables to the variables
	public function mapObject( $snip )
	{		
		$this->snippet_id 	= $snip[snippet_id];
		$this->snippet_title 	= $snip[snippet_title];
		$this->snippet_code 	= $snip[snippet_code];
		$this->snippet_type 	= $snip[snippet_type];
		$this->snippet_created 	= $snip[snippet_created];
		$this->snippet_user 	= $snip[snippet_user];
	}
}
?>