Start Now
Which adapter adapts better to your adaption?

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).

marco:

People always ask me how they can make great coffee. I’ve never had a good universal answer until now.

The guys at 37signals got the message. Start Now, because you don’t know enough to know what can’t be done yet.

If you’re on Windows, I don’t begin to know how to help you, aside from suggesting Cygwin. Right or wrong, participating in the open-source front-end developer community is materially more difficult on a Windows machine. On the bright side, MacBook Airs are cheap, powerful, and ridiculously portable, and there’s always Ubuntu or another *nix.
(Re)installing PEAR on Mac OS X (Re: homebrew)

If you’re using homebrew, your native PEAR install will be here:

/usr/local/Cellar/php/5.3.10/lib/php/PEAR

I used to use go-pear, but it is so old now that it won’t work on newer versions of PHP.  Time to learn that command line.

brew install wget

wget http://pear.php.net/go-pear.phar

sudo php -d detect_unicode=0 go-pear.phar

Make sure to setup option 1 to point to the homebrew location of PEAR referenced above (I’ll reference here again in case you weren’t paying attention)

/usr/local/Cellar/php/5.3.10/lib/php/PEAR

The go-pear.phar installer should offer you the option to update your path for you.   If you’re silly enough to not let it, you can manually add it (/usr/local/etc/php.ini).

include_path=”/usr/local/Cellar/php/5.3.10/lib/php/PEAR/share/pear:.;/usr/local/Cellar/php/5.3.10/lib/php”

Be sure to restart apache for the php.ini changes to take effect.  Now you’re ready to make sure that PEAR is working.  Your best best is to follow the official steps on the
http://pear.php.net/manual/en/installation.checking.php
Homebrew(ed) Apache, MySQL, phpMyAdmin

At the time of this writing, I’m relatively new to the world of homebrew package management.  Chances are good that if you are reading this, you are too.  Have no fear these things are easier to install than you think, even if you hit a few bumps in the road.

Once you get the hang of homebrew you want to start installing all kinds of awesomeness.  I had already fired up the native install of Apache that comes with Mac OS 10.7 and was well on my way to serving up websites that didn’t need databases.  Then I ran into one that needed it.

I tried the usual suspect:

brew install mysql

to my delight, mysql was installed (pretty sweet right?).  Next up I installed phpMyAdmin (you gotta have a way to manage that database right).  This too magically installed, however once I made the required modification to the apache httpd.conf file and fired up phpMyAdmin through my browser I kept getting the “The mcrypt extension is missing” error.

If you are using Homebrew as your OSX package manager, chances are that you are using the default PHP that comes with Lion. This PHP installation does not have the mcrypt extension, so if you are trying to use something like PHPMyAdmin, you’ll get an error message saying that mcrypt is not enabled in php.ini. I stumbled on a few articles on this topic, which I list here in case you want to read them too.  Skip down for the solution:

Even though the re-install of PHP solved the lacking mcrypt issue, I still had an issue accessing phpMyAdmin.  I didn’t change any of the defaults, so I could connect fine with:

mysql -uroot -p

However, phpMyAdmin by default has a flag set in the config that prevents you from logging in with an empty password.  A good security measure to be sure, but to get in you ‘ll need to make the following change to the config.inc.php file

 36 $cfg[‘Servers’][$i][‘AllowNoPassword’] = true;

Now you should be able to fire up mysql

msyql.server start

Login to phpMyAdmin

http://localhost/phpmyadmin

via kellysutton:

layervault:

We’re trying to get better about open-sourcing some of the great code that we write. Our first foray into open-sourcing the cool stuff behind LayerVault is the simple yet effective jquery.data.filter.js.

It’s a simple jQuery plugin that makes it easier to filter DOM elements by data-*…

Some new hotness we just open-sourced.

Every wonder why you can, or opt not to, use semi colons in your javascript?  This article spells it out.  I personally opt for the best practice of always using semicolons (and opening and closing brackets for that matter).  What’s your take?

The iPad Junior

bigweek:

John’s comments from episode 86 of The Talk Show are getting some attention from a few of my favorite Apple-centric websites, like Mac Rumors, The Next Web, and The Tech Block.

Update: Also The Verge.

Just finished listening to Dan Benjamin and John Grubber discussing the finer points of what may affectionately become known as the iPad Jr. - I’d buy one in a second, where do I sing up?!

This was an interesting topic I came across last week.  The author seems to have a problem with merge “bubbles” and suggests eliminating them with the use of

git pull —rebase

—rebase is a self proclaimed “dangerous” tool as it works like a clever to rearrange history to your liking:

Rebase helps to cut up commits and slice them into any way that you want them served up, and placed exactly where you want them. You can actually rewrite history with this command, be it reordering commits, squashing them into bigger ones, or completely ignoring them if you so desire.

http://gitready.com/intermediate/2009/01/31/intro-to-rebase.html

Rather than hack history every time I want to make a commit, I’d rather just do it by the books.  What’s wrong with a bubble anyway?  Then again, I’m not the type to jailbreak my iPhone either