Friday, January 9, 2009

A Different Kind of New Year Reflection

I've been attempting to get down and dirty with Zend Framework lately. It has the a la carte design that I want from a framework, but Django is still my first love. Despite all of the libraries in Zend, it still doesn't have that 'batteries' included feel I get from Django. I mean do I really have to write my own admin and authorization? Hell even RoR let's me whip up some scaffolding.... (And no phpMyAdmin is not what I would consider an acceptable admin interface). Don't even get me started on the benchmarks or how awesome Django's documentation is... but this post is about Zend and PHP....

Zend does provide a decent table abstraction (screw you mysql/mysqli functions) with some ActiveRecord like capabilities i.e.:
$table = new MyTable(); //Assumes database adapter was initialized
$newEntry = array('name'=>"TK",'state'=>"TX");
$newEntry['id'] = $table->insert($newEntry);
$oldEntry = $table->fetchRow(
$table->select()->where("id = ?",$newEntry['id']))

Not too shabby, an ok query builder with auto-escaping, but I still feel like something is missing...

Thankfully PHP5 introduced some decent meta programming/reflection capabilities. Of particular interest is the "magic" function __call($method,$arguments).
class My_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
public function __call($method,$arguments){
if(strncmp($method,"getBy",5) == 0){
if(count($arguments) != 1)
throw new My_Exception("getBy(value) takes one argument");
$fieldName = strtolower(substr($method,5));
return $this->_getBy($fieldName,$arguments[0]);
throw new My_Exception("$this->_name has no field '$fieldName'");
throw new My_Exception("Unknown method: '$method'");
public function _getBy($col,$value){
$result = $this->fetchRow($this->select()->where("$col = ?",$value));
return (null == $result) ? null : $result->toArray();
$oldEntry = $table->getById($newEntry['id']);

I'm terrified I might start writing Django's ORM in PHP, but I really don't have the time. The eventual task of writing a Zend_Form for every table I create is just as daunting... maybe I just need to Google around some more


Wil Sinclair said...

Hmmm. . . the benchmarks you reference are all from pre-1.7. You might want to test performance for your own needs on 1.7.x. Obviously, a 2-year-old comparison isn't going to reflect the current state of affairs. :)
We'll be looking at implementing the ActiveRecord pattern once PHP has late static binding, but for now Db_Table is a solution that works for most and doesn't over0-promise. ;)
You might be interested in the upcoming 1.8 release, which will be focused on RAD tooling. I think you'll find that it offers the most advanced tooling available in any framework.

Thanks for the feedback!

Anonymous said...

порно со школьницами онлайн порно видео учительница трахнув школьницу без реэстрации онлайн [url=][/url]