Using Amfphp 1.9 with the Adobe Flex 2 SDK - Page 2
On this page
PHP Code
Creating your PHP services
DISCLAIMER: The php classes used in this tutorial are based off classes generated by Titantic Linux DAO Generator. Comments have been removed for readablility. The Datasource.php class is the only class generated by DAO GEN that did not need any modifications. The remaining classes generated by DAO GEN will not work with AmfPhp without modification.
Amfphp looks for your php services in the [PROJECT_HOME]/src/php/amfphp/services/ directory by default. You can modify this setting by changing the $servicesPath variable in the globals.php file under your [PROJECT_HOME]/src/php/amfphp/ directory. Methods prefixed with an underscore '_' are considered private and won't be accessible to your Flex application. We will store our php files using package style naming. Create the following directory structure under your [PROJECT_HOME]/src/php/amfphp/services/ directory.
- org
- amfphp
- tutorials
- amfphp
WARNING: Be careful when you cut-n-paste the php code snippets. Amfphp will throw an error if the php files begin with whitespace or carriage return. Make sure nothing preceeds your opening php tag.
The Datasource.php class is a helper class used for communicating with your database.
Open your text editor and create a new file. Cut-n-paste the php code below into your new file. Save the file as Datasource.php under your [PROJECT_HOME]/src/php/amfphp/services/org/amfphp/tutorials/ directory.
<?php
class Datasource
{
var $dbLink;
function Datasource($dbHost, $dbName, $dbuser, $dbpasswd)
{
$this->dbLink = mysql_connect ($dbHost, $dbuser, $dbpasswd);
mysql_select_db ($dbName, $this->dbLink);
}
function _execute($sql)
{
$result = mysql_query($sql, $this->dbLink);
$this->_checkErrors($sql);
return $result;
}
function _executeBlind($sql)
{
$result = mysql_query($sql, $this->dbLink);
return $result;
}
function _nextRow ($result)
{
$row = mysql_fetch_array($result);
return $row;
}
function _checkErrors($sql)
{
$err=mysql_error();
$errno=mysql_errno();
if($errno)
{
$message = "The following SQL command ".$sql." caused Database error: ".$err.".";
print "Unrecowerable error has occurred. All data will be logged.";
print "Please contact System Administrator for help! \n";
print "\n";
exit;
}
else
{
return;
}
}
}
?>
The Employee class is your value object. The $_explicitType variable informs Amfphp that this class maps to an actionscript class in your Flex application.
Open your text editor and create a new file. Cut-n-paste the php code below into your new file. Save the file as Employee.php under your [PROJECT_HOME]/src/php/amfphp/services/org/amfphp/tutorials/ directory.
<?php
class Employee
{
var $empId;
var $firstName;
var $lastName;
var $phone;
var $email;
var $title;
// explicit actionscript package
var $_explicitType = "org.amfphp.tutorials.Employee";
}
?>
The EmployeeDao.php class provides methods for preforming the basic CRUD operation. The save and remove methods both expected associative array's as parameters and return Employee objects.
Open your text editor and create a new file. Cut-n-paste the php code below into your new file. Save the file as EmployeeDao.php under your [PROJECT_HOME]/src/php/amfphp/services/org/amfphp/tutorials/ directory. Depending on how you setup your database you might need to modify the EmployeeDao contructor to reflect you database settings.
<?php
require_once('./Employee.php');
require_once('./Datasource.php');
class EmployeeDao
{
var $conn;
function EmployeeDao()
{
$this->conn = new Datasource("localhost", "test", "", "");
}
function load($empId)
{
if (!$empId)
{
return false;
}
$sql = "SELECT * FROM Employee WHERE (empId = ". $empId .") ";
return $this->_singleQuery($sql);
}
function loadAll()
{
$sql = "SELECT * FROM Employee ORDER BY empId ASC ";
$searchResults = $this->_listQuery($sql);
return $searchResults;
}
function _create($valueObject)
{
$sql = "INSERT INTO Employee ( firstName, lastName, phone, ";
$sql = $sql."email, title) VALUES ('".$valueObject[firstName]."', ";
$sql = $sql."'".$valueObject[lastName]."', ";
$sql = $sql."'".$valueObject[phone]."', ";
$sql = $sql."'".$valueObject[email]."', ";
$sql = $sql."'".$valueObject[title]."') ";
$result = $this->_databaseUpdate($sql);
$sql = "SELECT last_insert_id()";
$result = $this->conn->_execute($sql);
if ($row = $this->conn->_nextRow($result))
{
return $row[0];
}
else
{
return false;
}
}
function save($valueObject)
{
if( array_key_exists("empId",$valueObject) )
{
if ($valueObject[empId] == 0)
{
$insertId = $this->_create($valueObject);
if($insertId)
{
$temp = new Employee();
$temp->empId = $insertId;
$temp->firstName = $valueObject[firstName];
$temp->lastName = $valueObject[lastName];
$temp->phone = $valueObject[phone];
$temp->email = $valueObject[email];
$temp->title = $valueObject[title];
return $temp;
}
}
else
{
if($this->_update($valueObject))
{
$temp = new Employee();
$temp->empId = $valueObject[empId];
$temp->firstName = $valueObject[firstName];
$temp->lastName = $valueObject[lastName];
$temp->phone = $valueObject[phone];
$temp->email = $valueObject[email];
$temp->title = $valueObject[title];
return $temp;
}
}
}
return false;
}
function _update($valueObject)
{
$sql = "UPDATE Employee SET firstName = '".$valueObject[firstName]."', ";
$sql = $sql."lastName = '".$valueObject[lastName]."', ";
$sql = $sql."phone = '".$valueObject[phone]."', ";
$sql = $sql."email = '".$valueObject[email]."', ";
$sql = $sql."title = '".$valueObject[title]."'";
$sql = $sql." WHERE (empId = ".$valueObject[empId].") ";
$result = $this->_databaseUpdate($sql);
if ($result != 1)
{
return false;
}
return true;
}
function remove($valueObject)
{
if (!array_key_exists("empId",$valueObject) )
{
return false;
}
$sql = "DELETE FROM Employee WHERE (empId = ".$valueObject[empId].") ";
$result = $this->_databaseUpdate($sql);
if ($result != 1)
{
return false;
}
$temp = new Employee();
$temp->empId = $valueObject[empId];
$temp->firstName = $valueObject[firstName];
$temp->lastName = $valueObject[lastName];
$temp->phone = $valueObject[phone];
$temp->email = $valueObject[email];
$temp->title = $valueObject[title];
return $temp;
}
function removeAll()
{
$sql = "DELETE FROM Employee";
$result = $this->_databaseUpdate($sql);
return true;
}
function _databaseUpdate($sql)
{
$result = $this->conn->_execute($sql);
return $result;
}
function _singleQuery($sql)
{
$valueObject = new Employee();
$result = $this->conn->_execute($sql);
if ($row = $this->conn->_nextRow($result))
{
$valueObject->empId = $row[0];
$valueObject->firstName = $row[1];
$valueObject->lastName = $row[2];
$valueObject->phone = $row[3];
$valueObject->email = $row[4];
$valueObject->title = $row[5];
}
else
{
return false;
}
return $valueObject;
}
function _listQuery($sql)
{
$searchResults = array();
$result = $this->conn->_execute($sql);
while ($row = $this->conn->_nextRow($result))
{
$temp = new Employee();
$temp->empId = $row[0];
$temp->firstName = $row[1];
$temp->lastName = $row[2];
$temp->phone = $row[3];
$temp->email = $row[4];
$temp->title = $row[5];
array_push($searchResults, $temp);
}
return $searchResults;
}
}
?>