Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created January 23, 2009 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipertracker/50998 to your computer and use it in GitHub Desktop.
Save hipertracker/50998 to your computer and use it in GitHub Desktop.
<?php
class Source {}
class DbSource extends Source {
function __construct($cfg) {
$this->conn = new PDO($cfg['adapter'].":dbname=".$cfg['db'].";host=".$cfg['host'], $cfg['user'], $cfg['password']);
}
// Mapping properties - db_columns
private function property_names() {
$result = array();
foreach ($this->schema['property'] as $name => $opts) {
$result[ empty($opts['name']) ? $name : $opts['name'] ] = $name;
}
return $result;
}
private function pk_name() {
foreach ($this->property_names() as $k => $v) {
if ($v == 'id') {
return $k;
}
}
return null;
}
// Returns records with property names instead of original db column names
private function as_valid_properties($rows) {
$result = null;
$properties = $this->property_names();
foreach ($rows as $row) {
$obj = new stdClass();
foreach ($row as $name => $value) {
if (in_array($name, array_keys($properties))) {
$property_name = $properties[$name];
$obj->$property_name = $value;
}
}
$result[] = $obj;
}
return $result;
}
private function materialize($sql, $fetch_one) {
$sth = $this->conn->prepare($sql);
$sth->execute();
if ($fetch_one) {
$result = $this->as_valid_properties(array($sth->fetch(PDO::FETCH_ASSOC)));
} else {
$result = $this->as_valid_properties($sth->fetchAll(PDO::FETCH_ASSOC));
}
return $fetch_one ? $result[0] : $result;
}
// PUBLIC
function immerse($that) {
$this->child = $that;
$this->schema = $this->child->schema;
}
function methods() {
return array('all', 'first', 'last');
}
function all() {
return $this->materialize("SELECT * FROM ".$this->schema['table_name'], false);
}
function first() {
return $this->materialize("SELECT * FROM ".$this->schema['table_name']." LIMIT 1", true);
}
function last() {
return $this->materialize("SELECT * FROM ".$this->schema['table_name']." ORDER BY ".$this->pk_name()." DESC LIMIT 1", true);
}
}
abstract class AbstractModel {
function __construct(Source $src) {
$this->sql = array();
$this->src = $src;
$src->immerse($this);
}
function __call($name, $arguments=array()) {
return $this->src->$name();
}
}
class PlayerModel extends AbstractModel {
function __construct(Source $opts) { parent::__construct($opts); }
public $schema = array(
'table_name' => 'players',
'property' => array('id' => array('value' => null),
'key' => array('value' => null, 'name' => 'player_key'),
'player' => array('value' => null, 'name' => 'player_name')));
}
class ValidCountryModel extends AbstractModel {
function __construct(Source $opts) { parent::__construct($opts); }
public $schema = array(
'table_name' => 'valid_countries',
'property' => array('id' => array('value' => null),
'country' => array('value' => null),
'shipping_code' => array('value' => null)));
}
// USAGE
$cfg = array(
'adapter' => 'mysql',
'host' => '127.0.0.1',
'db' => 'dbname',
'user' => 'user',
'password' => 'passwd',
);
$obj = new PlayerModel(new DbSource($cfg));
print_r($obj->first());
print_r($obj->last());
print_r($obj->all());
$obj = new ValidCountryModel(new DbSource($cfg));
print_r($obj->all());
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment