Full Service Class For Amfphp
Join the DZone community and get the full member experience.
Join For FreeHere 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:
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
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];
}
}
?>
Opinions expressed by DZone contributors are their own.
Comments