The Zend Framework Preview edition is out. I had look this morning at Zend_Db_DataObject, which is an implementation of the Active Record pattern. After reading the tutorial, decided to implement something similar for ADOdb. After a couple of hours coding, I had an implementation that works with both PHP4 and PHP5, and provides a superset of the functionality described in the above link.
The Active Record pattern is becoming extremely popular because it allows you to hide many implementation details for loading, inserting and updating data. For example, the following code reads a record from the Products table, and allows you to update some fields without SQL. Works in both PHP4 and 5!
// setup
ADODB_Active_Record::SetDatabaseAdapter($db);
class Product extends ADODB_Active_Record {};
// Example1: create new record, then update
$activeRec = new Product(); // access Products table
$activeRec->name = 'New Name';
$activeRec->price = 43.90;
$activeRec->save(); // perform insert
$activeRec->name = 'Renamed';
$activeRec->save(); // update the same record
// Example 2: load existing record, then update
$activeRec2 = new Product();
$activeRec2->Load('id=4'); // load record using SELECT * FROM Products WHERE id=4
$activeRec2->price *= 1.1; // change price
$activeRec2->save(); // perform update
// return an array of ActiveRecords for processing...
$activeRecArray = $db->GetActiveRecords($table,"name like 'A%'");
foreach($activeRecArray as $rec) {
...
}
Highlights:
- Works with PHP4 and PHP5 and provides equivalent functionality in both versions of PHP. Zend_Db_DataObject only works with PHP5.
- Function names similar to Zend_Db_DataObject for easy porting, with extensions such as Load() and Replace().
- ADodb_Active_Record works when you are connected to multiple databases. Zend_Db_DataObject only works when connected to a default database.
- Support for $ADODB_ASSOC_CASE. The field names are upper-cased, lower-cased or left in natural case depending on this setting.
- No field name conversion to camel-caps style, unlike Zend's implementation which will convert field names such as 'first_name' to 'firstName'.
- New ADOConnection::GetActiveRecords() and ADOConnection::GetActiveRecordsClass() functions in adodb.inc.php.
- Caching of table metadata so it is only queried once per table, no matter how many Active Records are created.
Try ADOdb's Active Record Implementation!
Read the tutorial. This implementation has been incorporated in the main ADOdb code branch since 4.80. Download ADOdb.
Enjoy!
PS: I noticed that the Preview release lacks the source code of the Zend_Db_DataObject implementation. I wonder why?
![]()

