Using AMFPHP with ADOdb
Very often you want to add functionality from a PHP library to your AMFPHP service classes. This short tutorial shows the use of ADOdb as database abstraction layer and may be especially helpful for beginners with AMFPHP.
ADOdb is a widely used database abstraction library that supports a lot of databases. If you want to add cross-database support to your Flex application, ADOdb is an excellent choice. But even if you only use MySQL as your bread-and-butter database, ADOdb will greatly help you to simplify your database operations.
Folder Structure
First, let’s take a look at the folder structure on the server. I usually organize my server directories like this:

There is an includes folder on the same level as the amfphp installation folder. The includes folder contains other folders, one for each PHP library (in this case ADOdb, PHPMailer and TCPDF). Download the ADOdb library from here and upload the files to the includes/adodb folder.
globals.php
It is recommended that you add globally used variables to AMFPHP’s globals.php file. Since most of your service classes probably need access to the database this file is the best place for configuring your database settings and the path to your external PHP libraries. So let’s add these lines to the globals.php file:
-
define('DB_HOST', 'localhost');
-
define('DB_USER', 'dbuser');
-
define('DB_PASSWORD', 'dbpassword');
-
define('DB_NAME', 'dbname');
-
define('INCLUDES_PATH', '/base/server/path/includes/');
Of course, you have to adjust the database and path settings to your needs. The INCLUDES_PATH constant should hold the absolute path to your includes directory on the server.
Sample Classes
Let’s say you have a MySQL database that holds a table with product data. For the sake of simplicity, we assume that there are only two fields in this table, product_id and product_name.
If you use the Cairngorm framework for your Flex application you are familiar with the concept of Value Objects (VOs). Instead of using XML or simple arrays, VOs allow for sending back and forth typed objects between Flex and AMFPHP. We define a simple ProductVO class and place the ProductVO.php file in AMFPHP’s services folder.
-
<?php
-
class ProductVO
-
{
-
var $_explicitType = 'ProductVO';
-
var $productId;
-
var $productName;
-
}
-
?>
Now we add a service class (ProductsService.php) to the services folder. The ProductsService.php file includes the ProductVO class and the ADOdb library. The constructor establishes the database connection (for the syntax take a look at the ADOdb documentation), and the getProducts() mehod retrieves all products from the database, wraps them into ProductVO objects and sends them back to your Flex application.
-
<?php
-
require_once(INCLUDES_PATH.'adodb/adodb.inc.php');
-
require_once('ProductVO.php');
-
-
class ProductsService
-
{
-
var $db;
-
-
/**
-
* Constructor
-
*/
-
function ProductsService()
-
{
-
$this->db = NewADOConnection('mysql');
-
$this->db->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
-
if (!$this->db) die('Connection failed');
-
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
-
}
-
-
/**
-
* This method retrieves all products from the database
-
*/
-
function getProducts()
-
{
-
$products = array();
-
$sql = 'SELECT product_id, product_name FROM products';
-
$result = $this->db->Execute($sql);
-
if ($result)
-
{
-
while (!$result->EOF)
-
{
-
$productVO = new ProductVO();
-
$productVO->productId = $result->fields['product_id'];
-
$productVO->productName = $result->fields['product_name'];
-
array_push($products, $productVO);
-
$result->MoveNext();
-
}
-
}
-
return $products;
-
}
-
-
}
-
?>
In a similar way you can add other PHP libraries to your AMFPHP classes (like the above mentioned PHPMailer and TCPDF libraries). I hope this example helps you to utilize the power of some of the most popular PHP libraries for your Flex applications. If you have suggestions or requests please comment or get in touch with me.
May 6th, 2008 at 7:25 pm
Danke Stefan
Nicely written. This has been on my radar for database use. I may try to get this to work on WebORB/PHP as their 3.0 version seems more stable but has trouble with MySQL.