ADOdb Active Record

(c) 2000-2006 John Lim (jlim#natsoft.com)

This software is dual licensed using BSD-Style and LGPL. This means you can use it in compiled proprietary and commercial products.


  1. Introduction
  2. ADOdb_Active_Record is an Object Relation Mapping (ORM) implementation using PHP. In an ORM system, the tables and rows of the database are abstracted into native PHP objects. This allows the programmer to focus more on manipulating the data and less on writing SQL queries.

    This implementation differs from Zend Framework's implementation in the following ways:

    ADOdb_Active_Record is designed upon the principles of the "ActiveRecord" design pattern, which was first described by Martin Fowler. The ActiveRecord pattern has been implemented in many forms across the spectrum of programming languages. ADOdb_Active_Record attempts to represent the database as closely to native PHP objects as possible.

    ADOdb_Active_Record maps a database table to a PHP class, and each instance of that class represents a table row. Relations between tables can also be defined, allowing the ADOdb_Active_Record objects to be nested.

  3. Setting the Database Connection
  4. The first step to using ADOdb_Active_Record is to set the default connection that an ADOdb_Active_Record objects will use to connect to a database.

    require_once('adodb/adodb-active-record.inc.php');
    
    $db = NewADOConnection('mysql://root:pwd@localhost/dbname');
    ADOdb_Active_Record::SetDatabaseAdapter($db);
    

  5. Table Rows as Objects
  6. First, let's create a temporary table in our MySQL database that we can use for demonstrative purposes throughout the rest of this tutorial. We can do this by sending a CREATE query:

    $db->Execute("CREATE TEMPORARY TABLE `persons` (
                    `id` int(10) unsigned NOT NULL auto_increment,
                    `name_first` varchar(100) NOT NULL default '',
                    `name_last` varchar(100) NOT NULL default '',
                    `favorite_color` varchar(100) NOT NULL default '',
                    PRIMARY KEY  (`id`)
                ) ENGINE=MyISAM;
               ");
     

    ADOdb_Active_Record's are object representations of table rows. Each table in the database is represented by a class in PHP. To begin working with a table as a ADOdb_Active_Record, a class that extends ADOdb_Active_Records needs to be created for it.

    class Person extends ADOdb_Active_Record{}
    $person = new Person();
    

    In the above example, a new ADOdb_Active_Record object $person was created to access the "persons" table. Zend_Db_DataObject takes the name of the class, pluralizes it (according to American English rules), and assumes that this is the name of the table in the database.

    This kind of behavior is typical of ADOdb_Active_Record. It will assume as much as possible by convention rather than explicit configuration. In situations where it isn't possible to use the conventions that ADOdb_Active_Record expects, options can be overridden as we'll see later.

  7. Table Columns as Object Properties
  8. When the $person object was instantiated, ADOdb_Active_Record read the table metadata from the database itself, and then exposed the table's columns (fields) as object properties.

    Our "persons" table has three fields: "name_first", "name_last", and "favorite_color". Each of these fields is now a property of the $person object. To see all these properties, use the ADOdb_Active_Record::getAttributeNames() method:

    var_dump($person->getAttributeNames());
    
    /**
     * Outputs the following:
     * array(4) {
     *    [0]=>
     *    string(2) "id"
     *    [1]=>
     *    string(9) "name_first"
     *    [2]=>
     *    string(8) "name_last"
     *    [3]=>
     *    string(13) "favorite_color"
     *  }
     */
        

    One big difference between ADOdb and Zend's implementation is we do not automatically convert to camelCaps style.

  9. Inserting and Updating a Record
  10. An ADOdb_Active_Record object is a representation of a single table row. However, when our $person object is instantiated, it does not reference any particular row. It is a blank record that does not yet exist in the database. An ADOdb_Active_Record object is considered blank when its primary key is NULL. The primary key in our persons table is "id".

    To insert a new record into the database, change the object's properties and then call the ADOdb_Active_Record::save() method:

    $person = new Person();
    $person->nameFirst = 'Andi';
    $person->nameLast  = 'Gutmans';
    $person->save();
     

    Oh, no! The above code snippet does not insert a new record into the database. Instead, outputs an error:

    1048: Column 'name_first' cannot be null
     

    This error occurred because MySQL rejected the INSERT query that was generated by ADOdb_Active_Record. If exceptions are enabled in ADOdb and you are using PHP5, an error will be thrown. In the definition of our table, we specified all of the fields as NOT NULL; i.e., they must contain a value.

    ADOdb_Active_Records are bound by the same contraints as the database tables they represent. If the field in the database cannot be NULL, the corresponding property in the ADOdb_Active_Record also cannot be NULL. In the example above, we failed to set the property $person->favoriteColor, which caused the INSERT to be rejected by MySQL.

    To insert a new ADOdb_Active_Record in the database, populate all of ADOdb_Active_Record's properties so that they satisfy the constraints of the database table, and then call the save() method:

    /**
     * Calling the save() method will successfully INSERT
     * this $person into the database table.
     */
    $person = new Person();
    $person->name_first     = 'Andi';
    $person->name_last      = 'Gutmans';
    $person->favorite_color = 'blue';
    $person->save();
    

    Once this $person has been INSERTed into the database by calling save(), the primary key can now be read as a property. Since this is the first row inserted into our temporary table, its "id" will be 1:

    var_dump($person->id);
    
    /**
     * Outputs the following:
     * string(1)
     */
     

    From this point on, updating it is simply a matter of changing the object's properties and calling the save() method again:

    $person->favorite_color = 'red';
    $person->save();
       

    The code snippet above will change the favorite color to red, and then UPDATE the record in the database.

    ADOdb Specific Functionality

  11. Setting the Table Name
  12. The default behaviour on creating an ADOdb_Active_Record is to "pluralize" the class name and use that as the table name. Often, this is not the case. For example, the Person class could be reading from the "People" table.

    We provide two ways to define your own table:

    1. Use a constructor parameter to override the default table naming behaviour.

    	class Person extends ADOdb_Active_Record{}
    	$person = new Person('People');
    

    2. Define it in a class declaration:

    	class Person extends ADOdb_Active_Record
    	{
    	var $_table = 'People';
    	}
    	$person = new Person();
    

  13. $ADODB_ASSOC_CASE
  14. This allows you to control the case of field names and properties. For example, all field names in Oracle are upper-case by default. So you can force field names to be lowercase using $ADODB_ASSOC_CASE. Legal values are as follows:

     0: lower-case
     1: upper-case
     2: native-case
    

    So to force all Oracle field names to lower-case, use

    $ADODB_ASSOC_CASE = 0;
    $person = new Person('People');
    $person->name = 'Lily';
    $ADODB_ASSOC_CASE = 2;
    $person2 = new Person('People');
    $person2->NAME = 'Lily'; 
    

    Also see $ADODB_ASSOC_CASE.

  15. ADOdb_Active_Record::Save()
  16. Saves a record by executing an INSERT or UPDATE SQL statement as appropriate.

    Returns false on unsuccessful INSERT, true if successsful INSERT.

    Returns 0 on failed UPDATE, and 1 on UPDATE if data has changed, and -1 if no data was changed, so no UPDATE statement was executed.

  17. ADOdb_Active_Record::Replace()
  18. ADOdb supports replace functionality, whereby the record is inserted if it does not exists, or updated otherwise.

    $rec = new ADOdb_Active_Record("product");
    $rec->name = 'John';
    $rec->tel_no = '34111145';
    $ok = $rec->replace(); // 0=failure, 1=update, 2=insert
    

  19. ADOdb_Active_Record::Load($where)
  20. Sometimes, we want to load a single record into an Active Record. We can do so using:

    $person->load("id=3");
    
    // or using bind parameters
    
    $person->load("id=?", array(3));
    

    Returns false if an error occurs.

  21. ADOdb_Active_Record::Find($whereOrderBy, $bindarr=false, $pkeyArr=false)
  22. We want to retrieve an array of active records based on some search criteria. For example:

    class Person extends ADOdb_Active_Record {
    var $_table = 'people';
    }
    
    $person = new Person();
    $peopleArray =& $person->Find("name like ? order by age", array('Sm%'));
    

  23. Error Handling and Debugging
  24. In PHP5, if adodb-exceptions.inc.php is included, then errors are thrown. Otherwise errors are handled by returning a value. False by default means an error has occurred. You can get the last error message using the ErrorMsg() function.

    To check for errors in ADOdb_Active_Record, do not poll ErrorMsg() as the last error message will always be returned, even if it occurred several operations ago. Do this instead:

    # right!
    $ok = $rec->Save();
    if (!$ok) $err = $rec->ErrorMsg();
    
    # wrong :(
    $rec->Save();
    if ($rec->ErrorMsg()) echo "Wrong way to detect error";
    

    The ADOConnection::Debug property is obeyed. So if $db->debug is enabled, then ADOdb_Active_Record errors are also outputted to standard output and written to the browser.

  25. ADOdb_Active_Record::Set()
  26. You can convert an array to an ADOdb_Active_Record using Set(). The array must be numerically indexed, and have all fields of the table defined in the array. The elements of the array must be in the table's natural order too.

    $row = $db->GetRow("select * from tablex where id=$id");
    
    # PHP4 or PHP5 without enabling exceptions
    $obj =& new ADOdb_Active_Record('Products');
    if ($obj->ErrorMsg()){
    	echo $obj->ErrorMsg();
    } else {
    	$obj->Set($row);
    }
    
    # in PHP5, with exceptions enabled:
    
    include('adodb-exceptions.inc.php');
    try {
    	$obj =& new ADOdb_Active_Record('Products');
    	$obj->Set($row);
    } catch(exceptions $e) {
    	echo $e->getMessage();
    }
    

  27. Primary Keys
  28. ADOdb_Active_Record does not require the table to have a primary key. You can insert records for such a table, but you will not be able to update nor delete.

    Sometimes you are retrieving data from a view or table that has no primary key, but has a unique index. You can dynamically set the primary key of a table through the constructor, or using ADOdb_Active_Record::SetPrimaryKeys():

    	$pkeys = array('category','prodcode');
    	
    	// set primary key using constructor
    	$rec = new ADOdb_Active_Record('Products', $pkeys);
    	
    	 // or use method
    	$rec->SetPrimaryKeys($pkeys);
    

  29. Retrieval of Auto-incrementing ID
  30. When creating a new record, the retrieval of the last auto-incrementing ID is not reliable for databases that do not support the Insert_ID() function call (check $connection->hasInsertID). In this case we perform a SELECT MAX($primarykey) FROM $table, which will not work reliably in a multi-user environment. You can override the ADOdb_Active_Record::LastInsertID() function in this case.

  31. Dealing with Multiple Databases
  32. Sometimes we want to load data from one database and insert it into another using ActiveRecords. This can be done using the optional parameter of the ADOdb_Active_Record constructor. In the following example, we read data from db.table1 and store it in db2.table2:

    $db = NewADOConnection(...);
    $db2 = NewADOConnection(...);
    
    ADOdb_Active_Record::SetDatabaseAdapter($db2);
    
    $activeRecs = $db->GetActiveRecords('table1');
    
    foreach($activeRecs as $rec) {
    	$rec2 = new ADOdb_Active_Record('table2',$db2);
    	$rec2->id = $rec->id;
    	$rec2->name = $rec->name;
    	
    	$rec2->Save();
    }
    

    If you have to pass in a primary key called "id" and the 2nd db connection in the constructor, you can do so too:

    $rec = new ADOdb_Active_Record("table1",array("id"),$db2);
    

  33. $ADODB_ACTIVE_CACHESECS
  34. You can cache the table metadata (field names, types, and other info such primary keys) in $ADODB_CACHE_DIR (which defaults to /tmp) by setting the global variable $ADODB_ACTIVE_CACHESECS to a value greater than 0. This will be the number of seconds to cache. You should set this to a value of 30 seconds or greater for optimal performance.

  35. Active Record Considered Bad?
  36. Although the Active Record concept is useful, you have to be aware of some pitfalls when using Active Record. The level of granularity of Active Record is individual records. It encourages code like the following, used to increase the price of all furniture products by 10%:

     $recs = $db->GetActiveRecords("Products","category='Furniture'");
     foreach($recs as $rec) {
        $rec->price *= 1.1; // increase price by 10% for all Furniture products
        $rec->save();
     }
    
    Of course a SELECT statement is superior because it's simpler and much more efficient (probably by a factor of x10 or more):
       $db->Execute("update Products set price = price * 1.1 where category='Furniture'");
    

    Another issue is performance. For performance sensitive code, using direct SQL will always be faster than using Active Records due to overhead and the fact that all fields in a row are retrieved (rather than only the subset you need) whenever an Active Record is loaded.

  37. Transactions
  38. The default transaction mode in ADOdb is autocommit. So that is the default with active record too. The general rules for managing transactions still apply. Active Record to the database is a set of insert/update/delete statements, and the db has no knowledge of active records.

    Smart transactions, that does an auto-rollback if an error occurs, is still the best method to multiple activities (inserts/updates/deletes) that need to be treated as a single transaction:

    $conn->StartTrans();
    $parent->save();
    $child->save();
    $conn->CompleteTrans();
    

    ADOConnection Supplement

  39. ADOConnection::GetActiveRecords()
  40. This allows you to retrieve an array of ADOdb_Active_Records. Returns false if an error occurs.

    $table = 'products';
    $whereOrderBy = "name LIKE 'A%' ORDER BY Name";
    $activeRecArr = $db->GetActiveRecords($table, $whereOrderBy);
    foreach($activeRecArr as $rec) {
    	$rec->id = rand();
    	$rec->save();
    }
    

    And to retrieve all records ordered by specific fields:

    $whereOrderBy = "1=1 ORDER BY Name";
    $activeRecArr = $db->ADOdb_Active_Records($table);
    

    To use bind variables (assuming ? is the place-holder for your database):

    $activeRecArr = $db->GetActiveRecords($tableName, 'name LIKE ?',
    						array('A%'));
    

    You can also define the primary keys of the table by passing an array of field names:

    $activeRecArr = $db->GetActiveRecords($tableName, 'name LIKE ?',
    						array('A%'), array('id'));
    

  41. ADOConnection::GetActiveRecordsClass()
  42. This allows you to retrieve an array of objects derived from ADOdb_Active_Records. Returns false if an error occurs.

    class Product extends ADOdb_Active_Records{};
    $table = 'products';
    $whereOrderBy = "name LIKE 'A%' ORDER BY Name";
    $activeRecArr = $db->GetActiveRecordsClass('Product',$table, $whereOrderBy);
    
    # the objects in $activeRecArr are of class 'Product'
    foreach($activeRecArr as $rec) {
    	$rec->id = rand();
    	$rec->save();
    }
    

    To use bind variables (assuming ? is the place-holder for your database):

    $activeRecArr = $db->GetActiveRecordsClass($className,$tableName, 'name LIKE ?',
    						array('A%'));
    

    You can also define the primary keys of the table by passing an array of field names:

    $activeRecArr = $db->GetActiveRecordsClass($className,$tableName, 'name LIKE ?',
    						array('A%'), array('id'));
    

Code Sample

The following works with PHP4 and PHP5

include('../adodb.inc.php');
include('../adodb-active-record.inc.php');

// uncomment the following if you want to test exceptions
#if (PHP_VERSION >= 5) include('../adodb-exceptions.inc.php');

$db = NewADOConnection('mysql://root@localhost/northwind');
$db->debug=1;
ADOdb_Active_Record::SetDatabaseAdapter($db);

$db->Execute("CREATE TEMPORARY TABLE `persons` (
                `id` int(10) unsigned NOT NULL auto_increment,
                `name_first` varchar(100) NOT NULL default '',
                `name_last` varchar(100) NOT NULL default '',
                `favorite_color` varchar(100) NOT NULL default '',
                PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM;
           ");
		   
class Person extends ADOdb_Active_Record{}
$person = new Person();

echo "<p>Output of getAttributeNames: ";
var_dump($person->getAttributeNames());

/**
 * Outputs the following:
 * array(4) {
 *    [0]=>
 *    string(2) "id"
 *    [1]=>
 *    string(9) "name_first"
 *    [2]=>
 *    string(8) "name_last"
 *    [3]=>
 *    string(13) "favorite_color"
 *  }
 */

$person = new Person();
$person->nameFirst = 'Andi';
$person->nameLast  = 'Gutmans';
$person->save(); // this save() will fail on INSERT as favorite_color is a must fill...


$person = new Person();
$person->name_first     = 'Andi';
$person->name_last      = 'Gutmans';
$person->favorite_color = 'blue';
$person->save(); // this save will perform an INSERT successfully

echo "<p>The Insert ID generated:"; print_r($person->id);

$person->favorite_color = 'red';
$person->save(); // this save() will perform an UPDATE

$person = new Person();
$person->name_first     = 'John';
$person->name_last      = 'Lim';
$person->favorite_color = 'lavender';
$person->save(); // this save will perform an INSERT successfully

// load record where id=2 into a new ADOdb_Active_Record
$person2 = new Person();
$person2->Load('id=2');
var_dump($person2);

// retrieve an array of records
$activeArr = $db->GetActiveRecordsClass($class = "Person",$table = "persons","id=".$db->Param(0),array(2));
$person2 =& $activeArr[0];
echo "<p>Name first (should be John): ",$person->name_first, "<br>Class = ",get_class($person2);	

Todo (Code Contributions welcome)

Check _original and current field values before update, only update changes. Also if the primary key value is changed, then on update, we should save and use the original primary key values in the WHERE clause!

Handle 1-to-many relationships.

PHP5 specific: Make GetActiveRecords*() return an Iterator.

PHP5 specific: Change PHP5 implementation of Active Record to use __get() and __set() for better performance.

Change Log

0.05

If inserting a record and the value of a primary key field is null, then we do not insert that field in as we assume it is an auto-increment field. Needed by mssql.

0.04 5 June 2006

Added support for declaring table name in $_table in class declaration. Thx Bill Dueber for idea.

Added find($where,$bindarr=false) method to retrieve an array of active record objects.

0.03
- Now we only update fields that have changed, using $this->_original.
- We do not include auto_increment fields in replace(). Thx Travis Cline
- Added ADODB_ACTIVE_CACHESECS.

0.02
- Much better error handling. ErrorMsg() implemented. Throw implemented if adodb-exceptions.inc.php detected.
- You can now define the primary keys of the view or table you are accessing manually.
- The Active Record allows you to create an object which does not have a primary key. You can INSERT but not UPDATE in this case. - Set() documented.
- Fixed _pluralize bug with y suffix.

0.01 6 Mar 2006
- Fixed handling of nulls when saving (it didn't save nulls, saved them as '').
- Better error handling messages.
- Factored out a new method GetPrimaryKeys().

0.00 5 Mar 2006
1st release