7. AMFPHP
First take a look at the PHP "SongService.php" code (into /amfphp/services/it/sephiroth/ folder):
<?php
class SongService
{
var $methodTable;
function SongService()
{
$this->methodTable = array(
"getData" => array(
"description" => "Get songs list",
"access" => "remote",
"arguments" => array(),
"return" => "RecordSet"
),
"saveData" => array(
"description" => "Update songs list",
"access" => "remote",
"arguments" => array("packet"=>"XML"),
"return" => "Boolean"
),
);
}
/**
* Return the table contents
*/
function getData(){
if(!$conn_id = SongService::Connect()){
return false;
}
$query = sprintf("SELECT id_song, artist, title, genre FROM songs ORDER BY id_song ASC");
return mysql_query($query);
}
/**
* Receive an XML updatePacket and apply changes to the DB
*/
function saveData($packet){
if(!$conn_id = SongService::Connect()){
return false;
}
$dom = new DOMDocument($packet);
$doc = $dom->document_element();
$table_name = $doc->get_attribute("tableName");
$nodes = $doc->get_elements_by_tagname("update");
foreach($nodes as $node)
{
$fields = $node->get_elements_by_tagname("field");
$stmt = array();
$cond_stmt = "WHERE 1 = 1 ";
foreach($fields as $field)
{
if($field->get_attribute("key") == "true")
{
$fname = $field->get_attribute("name");
$ftype = $field->get_attribute("type");
$fold = $field->get_attribute("oldValue");
$fnew = $field->get_attribute("newValue");
if($ftype == "String")
{
$fold = "'".mysql_escape_string($fold)."'";
$fnew = "'".mysql_escape_string($fnew)."'";
}
$cond_stmt .= "AND $fname = $fold ";
if($field->has_attribute("newValue"))
{
$stmt[] = "$fname = $fnew";
}
}
}
$update_stmt = implode(", ", $stmt);
$query = "UPDATE $table_name SET $update_stmt $cond_stmt LIMIT 1";
mysql_query($query);
}
// DELETE
$nodes = $doc->get_elements_by_tagname("delete");
foreach($nodes as $node)
{
$fields = $node->get_elements_by_tagname("field");
$stmt = array();
$cond_stmt = "WHERE 1 = 1 ";
foreach($fields as $field)
{
if($field->get_attribute("key") == "true")
{
$fname = $field->get_attribute("name");
$ftype = $field->get_attribute("type");
$fold = $field->get_attribute("oldValue");
if($ftype == "String")
{
$fold = "'".mysql_escape_string($fold)."'";
}
$cond_stmt .= "AND $fname = $fold ";
}
}
$update_stmt = implode(", ", $stmt);
$query = "DELETE FROM $table_name $cond_stmt LIMIT 1";
mysql_query($query);
}
// INSERT
$nodes = $doc->get_elements_by_tagname("insert");
foreach($nodes as $node)
{
$fields = $node->get_elements_by_tagname("field");
$f_stmt = array();
$v_stmt = array();
foreach($fields as $field)
{
if($field->get_attribute("key") == "true")
{
$fname = $field->get_attribute("name");
$ftype = $field->get_attribute("type");
$fnew = $field->get_attribute("newValue");
if($ftype == "String")
{
$fnew = "'".mysql_escape_string($fnew)."'";
}
if($fname != "id_song")
{
$f_stmt[] = $fname;
$v_stmt[] = $fnew;
}
}
}
$values_stmt = implode(", ", $v_stmt);
$fields_stmt = implode(", ", $f_stmt);
$query = "INSERT INTO $table_name ($fields_stmt) VALUES($values_stmt)";
mysql_query($query);
}
return true;
}
/**
* Connect to DataBase
* @return connection id
*/
function Connect(){
$conn_id = @mysql_connect("localhost", "username", "password");
if($conn_id){
if(@mysql_select_db("my_database")){
return $conn_id;
} else {
return false;
}
} else {
return false;
}
}
}
?>
The class method table has just 2 items used by AMFPHP: getData and saveData. The Connnect function just create a connection to the MySQL Database.
8. SongService::getData
This method just create a mysql query and returns it. Nothing else.
This method is called from the 1st flash Remoting connector, which will receive this query object as Flash RecordSet (then passed to the DataSet component's dataProvider)
9. SongService::saveData
This method receives as input parameter an XML string (the updatePacket XML) created by the RDBMSResolver and parse it using PHP's Dom functions.
Then we are going to divide the XML based on the actions to perform: INSERT, UPDATE and DELETE.
$doc->get_elements_by_tagname("update"); for example gives us all the nodes containing updates for existing records. The xml nodes contain all the data we need in order to create out queries. For example, this sub-node:
<field name="artist" type="String" oldValue="After" newValue="After Forever" key="true" />
contains the field table name (artist), the previous value (oldValue), the modified value (newValue) and the type (String). In this case, when I receive a type string I have to pass it to mysql_escape_string to be sure to have an crrectly escaped string.
Download all the files used in this tutorial
