Created
March 31, 2012 10:21
-
-
Save pawellenart/2261642 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once SITE_PATH . 'inc/lib/DatabaseDriver.class.php'; | |
abstract class BaseModel extends DatabaseDriver implements ArrayAccess { | |
protected $tablename = null; | |
private $schema; | |
private $attributes = array(); | |
private $is_new = true; | |
private $is_modified = false; | |
public function __construct() { | |
parent::__construct(); | |
require SITE_PATH . 'inc/schema/schema.php'; | |
$this->schema = $schema; | |
} | |
// real rollercoaster here | |
// dynamic rails-like find_by_* methods | |
public function __call($method, $argument) { | |
if (!preg_match('/^find_by_[a-z0-9_]+/', $method)) { | |
throw new Exception('Function doesn\'t exist in ' . get_class($this) . ' class'); | |
} | |
// $type = find_by_*something* | |
$type = substr($method, 8); | |
if (!in_array($type, $this->schema[$this->table()])) { | |
throw new Exception("No such column '$type' in table '{$this->table()}'"); | |
} | |
$this->attributes = $this->db->fetchArrayEx($this->table(), array($type => implode('', $argument))); | |
if (count($this->attributes) == 1 && in_array('0', array_keys($this->attributes))) { | |
$this->attributes = $this->attributes[0]; | |
} | |
$this->is_new = false; | |
return $this; | |
} | |
// get all records | |
public function all() { | |
$this->attributes = $this->db->fetchArrayEx($this->table(), array()); | |
$this->is_new = false; | |
return $this; | |
} | |
// get one | |
public function get($id) { | |
$this->attributes = $this->db->fetchRecordEx($this->table(), $id); | |
$this->is_new = false; | |
return $this; | |
} | |
// alias for get() | |
public function find($id) { | |
return $this->get($id); | |
} | |
// create a record | |
public function create($rec) { | |
$id = $this->db->insertEx($this->table(), $rec); | |
$this->is_new = false; | |
return true; | |
} | |
public function save() { | |
// updating exisiting record | |
if ($this->is_modified && !$this->is_new) { | |
$this->db->updateEx($this->table(), $this->attributes['id'], $this->attributes); | |
return true; | |
} | |
// creating new record | |
elseif ($this->is_modified && $this->is_new) { | |
return $this->create($this->attributes); | |
} else { | |
return false; | |
} | |
} | |
// delete a record on a condition | |
public function delete($condition) { | |
$this->db->deleteEx($this->table(), $condition); | |
return true; | |
} | |
// delete all records | |
public function clear() { | |
$this->db->deleteEx($this->table()); | |
return true; | |
} | |
public function length() { | |
return count($this->attributes); | |
} | |
// internal use | |
// a table name | |
private function table() { | |
if (is_null($this->tablename)) { | |
return $this->pluralize(get_class($this)); | |
} else { | |
return $this->tablename; | |
} | |
} | |
// ArrayAccess extensions | |
public function offsetSet($offset, $value) { | |
if (in_array($offset, $this->schema[$this->table()])) { | |
$this->attributes[$offset] = $value; | |
$this->is_modified = true; | |
} else { | |
throw new Exception("No such column: '$offset'"); | |
} | |
} | |
public function offsetExists($offset) { | |
return isset($this->attributes[$offset]); | |
} | |
public function offsetUnset($offset) { | |
unset($this->attributes[$offset]); | |
} | |
public function offsetGet($offset) { | |
return isset($this->attributes[$offset]) ? $this->attributes[$offset] : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment