Ask any developer that is just starting out with PHP and they’ll tell you how great the core set of functions are to interface with databases. Ask any developer that has been in the game for awhile and they’ll tell you about how the improvements that were introduced with the PDO (PHP Data Object) improved life ten fold.
The PDO provides a nice abstraction layer (along with all kinds of other goodies) to make database interaction even easier, but it still leaves some things lacking. I’m not going to to talk in detail about those today, but I am going to tell you where you can go from here to find a better solution.
When I first started programming with PHP (circa 2004) none of this goodness existed and life was a lot of copying and pasting. Then the PDO came along and then PEAR wrapped it with DB and later the improved MDB2 implementation. When I came to the party MDB2 was en vogue and I wrote my own wrapper for MDB2 to do some extra logging. It worked great for me for many years and life was good. However, when I started doing development in Zend Framework I was introduced to a couple of different new design patterns for interfacing with databases. Enter the Zend_DB_Adapter_Abstract and the Zend_DB_Adapter. Let’s take a look at each and you can decide which way to roll.
Zend_DB_Adapter_Abstract
There are a couple of ways to use the Zend_DB_Abstract. You can setup a driver class (typically an abstract or interface level) and build up on it.
class Users extends Zend_Db_Table_Abstract {
/**
* explicitly set the table you are interfacing with
* if you don't do this, it will use table that matches the name of your class
* in this case the table would be 'users'
*/
protected $_name = 'users';
/**
* Specify what DB drive you want to use to interface to the DB
* The Default Adapter uses PDO
*/
function __construct() {
Zend_Db_Table_Abstract::getDefaultAdapter();
}
function getUsers() {
$select = $this->select();
$select->where('id = ?', $userId);
$userRow = $this->fetchRow($select);
}
}
Alternatively, you can just call the specific version of the abstract adapter and specify the that you want at run time:
Zend_Db_Table::setDefaultAdapter($dbAdapter);
$bugTable = new Zend_Db_Table('bug');
Zend_DB_Adapter
There are a couple of implementation options when using the Zend_DB_Adapater. You can explicitly specify which type of DB you are interfacing with:
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
Or you can just use the Factory
// We don't need the following statement because the
// Zend_Db_Adapter_Pdo_Mysql file will be loaded for us by the Zend_Db
// factory method.
// require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
// Automatically load class Zend_Db_Adapter_Pdo_Mysql
// and create an instance of it.
$db = Zend_Db::factory('Pdo_Mysql', array( 'host' =gt; '127.0.0.1', 'username' =gt; 'webuser', 'password' =gt; 'xxxxxxxx', 'dbname' =gt; 'test' ));
Conclusion
There are a lot of different methods of using DB adapters with Zend. Don’t let it wear you down, just find the adapter that adapts best to your problem (or use the factory, it’s always a good fallback).