ADOdb Implementation of Active Record: cloning Zend_Db_DataObject

Original post provide by PHP Everywhere - By John Lim

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?

Previous Articles:
Scale-Up Or Scale-Out Your Database
The typical technique used in MySQL for massive parallelism is the Continuous Replication method where a single master db server has read-write access to the database, and the data changes are replicated over to the other database servers, which are read-only.
Phplondon Conference 2008
phplondon.org announce their third annual community conference to be held at Inmarsat, Old Street, London.This year the conference will run two tracks and include speakers such as Derick Rethans, Wez Furlong, Scott MacVicar and Zoe Slattery.We will also be holding an extended presentation and discussion on frameworks for PHP.Visit our conference site to register.
Php|tek 2008: Chicago
Join us to hear top speakers such as Derick Rethans (eZ Systems), Lucas Nelan (Facebook), Chris Shiflett (OmniTI), Eli White (Digg) and yes, even Terry Chay (Tagged). For the past two years, php|architect's spring conference has sold out weeks before the start date, so if you're interested in attending, don't delay, and be sure to sign up ASAP!
PHP Québec 2008 : Call For Speaker
PHP Quebec is pleased to announce the sixth edition of the PHP Quebec Conference. The Conference will take place in Montreal, Canada between March 12th and 14th, 2008. We are looking for speakers willing to share their expertise with Canadian and United States PHP professionals.
PHP London 2007
After the success of 2006 the PHP London user group is staging the UK's second dedicated PHP conference on Friday, 23 February 2007, in London. The conference will be a low-cost event, costing £50 for the day. Speakers include: Rasmus Lerdorf, Cal Evans, Simon Laws and Kevlin Henney.
More Articles:
Adding Google-like Search Operators To Simple Search Boxes
Give your users instant access to your most important information.
Interaktive PHP Shell
Jan Kneschke, der Entwickler meines bevorzugten WebServers lighttpd, hat sich nun dieser Problematik angenommen und hat eine PHP Shell entwickelt, die den eingegebenen PHP Code vor dem Ausführen prüft. Dies geschieht über die Tokenizer Erweiterung. So lässt sich vor dem Ausführen beispielsweise überprüfen ob bei einem Funktionsaufruf die jeweilige Funktion überhaupt existiert oder dass keine doppelten Funktionsnamen angelegt werden.
Share Your OPML
Über die Webseite http://share.opml.org/ kann man seine OPML Dateien veröffentlichen und damit die abonnierten RSS Feeds anderen Leuten zugänglich machen. Das ist im Prinzip nicht weiter spektakulär. Der nette Aspekt an der Sache ist, dass man sehen kann, welche Leute die gleichen Feeds abonniert haben und ob sich so möglicherweise noch weitere interessante Feeds entdecken lassen.
Merkwürdiger OSX Absturz [update]
Jetzt muss ich mal herausfinden, woran das liegen könnte. Ich teste gerade diverse locking-mechanismen um eine brauchbare Möglichkeit zu finden 'race conditions' zu vermeiden. In diesem Speziellen Script wollte ich einen Mechanismus testen, der über Semaphoren funktioniert - möglicherweise geht dabei etwas schief.
PHP @ FrOSCon Call For Papers
The event will take place on August 25th and 26th in St. Augustin (near Bonn), Germany. For the dedicated PHP talks program, the call for papers is open until June 4th. Talks are accepted in English and German language.

Leave a Reply