Skip navigation.

ADOdb Implementation of Active Record: cloning Zend_Db_DataObject

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?

Comment viewing options

Select your preferred way to display the comments and click 'Save settings' to activate your changes.

Date nitpicky

I look forward to trying this implementation out. A question, though. The date on the readme indicates you've been sitting on this for a year. Is that correct?

Oops, date is wrong. Should b

Oops, date is wrong. Should be 2006 :)

missing methods?

Fatal error: Call to undefined method ADODB_mysql::GetActiveRecordsClass() ... same for GetActiveRecords from the ADOdb's Active Record implementation tutorial. everything else is and works GREAT!

Link Outdated

As of adodb-4.80 this implementation is included and is in fact newer than the one linked here (002 vs 001).

The link here should be noted or removed.