Skip to content

Instantly share code, notes, and snippets.

@mikesimons
Created September 15, 2010 10:16
Show Gist options
  • Save mikesimons/580517 to your computer and use it in GitHub Desktop.
Save mikesimons/580517 to your computer and use it in GitHub Desktop.
<?php
set_include_path("/home/mike/Downloads/ZendFramework-1.10.7/library/:" . get_include_path());
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$options = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'testing'
);
$db = Zend_Db::factory('PDO_MYSQL', $options);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
// implement iterable
class Iammike_Db_Table_Select extends Zend_Db_Table_Select {
protected $_finders = array();
public function __construct(Zend_Db_Table_Abstract $table) {
parent::__construct($table);
// enumerate finder methods
$methods = get_class_methods($table);
foreach($methods as $method) {
if(preg_match('/(.*)Finder$/', $method, $matches)) {
$this->_finders[$matches[1]] = $method;
}
}
}
public function __call($method, $params) {
// modifier table
$modifiers = array(
'/(.*)(Starting)/' => function($q, $c, $p) { return $q->where("{$c} like ?", "%{$p[0]}"); },
'/(.*)(Ending)/' => function($q, $c, $p) { return $q->where("{$c} like ?", "{$p[0]}%"); },
'/(.*)(Over)/' => function($q, $c, $p) { return $q->where("{$c} > ?", $p[0]); },
'/(.*)(Under)/' => function($q, $c, $p) { return $q->where("{$c} < ?", $p[0]); },
'/(.*)(After)/' => function($q, $c, $p) { return $q->where("{$c} > ?", $p[0]); },
'/(.*)(Before)/' => function($q, $c, $p) { return $q->where("{$c} < ?", $p[0]); },
'/(.*)(Between)/' => function($q, $c, $p) { return $q->where("{$c} > ?", $p[0])->where("{$c} < ?", $p[1]); },
'/(.*)(EqualTo)/' => function($q, $c, $p) { return $q->where("{$c} = ?", $p[0]); }
);
// dispatch to finder if one matches
if(isset($this->_finders[$method])) {
array_unshift($params, clone $this);
return call_user_func_array(array($this->_table, $this->_finders[$method]), $params);
}
// allows for bare column names to be used for exact matches
$columns = $this->_table->info(Zend_Db_Table_Abstract::COLS);
if(in_array($method, $columns)) {
$method .= 'EqualTo';
}
// strip prefixes (with|by)
$method = preg_replace_callback(
array('/^with([a-zA-Z]{1})/', '/^by([a-zA-Z]{1})/'),
function($m) {
return strtolower($m[1]);
},
$method
);
// match and apply modifier if found
foreach($modifiers as $mod => $cb) {
if(preg_match($mod, $method, $matches)) {
return $cb(clone $this, $matches[1], $params);
}
}
// fall through to parent magic
return parent::__call($method, $params);
}
}
class Iammike_Db_Table_Abstract extends Zend_Db_Table_Abstract {
public function select()
{
$select = new Iammike_Db_Table_Select($this);
$select->from($this->info(self::NAME), Zend_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
// required for joins
$select->setIntegrityCheck(false);
return $select;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment