Skip to content

Instantly share code, notes, and snippets.

@pawellenart
Created May 30, 2012 17:43
Show Gist options
  • Save pawellenart/2837884 to your computer and use it in GitHub Desktop.
Save pawellenart/2837884 to your computer and use it in GitHub Desktop.
<?php
require_once SITE_PATH . 'inc/lib/DatabaseDriver.class.php';
abstract class BaseModel extends DatabaseDriver implements ArrayAccess, Iterator {
protected $tablename = null;
private $schema;
protected $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;
}
// 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 method '$method' in class '" . get_class($this) . "'");
}
$this->where(array('where' => array($type => implode('', $argument))));
return $this;
}
// get all records
public function all() {
$this->attributes = $this->db->fetchArrayEx($this->table(), array());
$this->is_new = false;
return $this;
}
// get records on conditions
public function where(array $conditions) {
$this->attributes = $this->db->fetchArrayEx($this->table(), $conditions);
if (count($this->attributes) == 1 && in_array('0', array_keys($this->attributes))) {
$this->attributes = $this->attributes[0];
}
//var_dump($this->attributes);
$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(array $attrs) {
$id = $this->db->insertEx($this->table(), $attrs);
$this->is_new = false;
return true;
}
// save record to the database
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 conditions
public function delete(array $conditions) {
$this->db->deleteEx($this->table(), $conditions);
return true;
}
// delete all records
public function clear() {
$this->db->deleteEx($this->table());
return true;
}
// length of results
public function length() {
if ($this->attributes == false) {
return 0;
}
if (!isset($this->attributes[0])) {
return 1;
}
return count($this->attributes);
}
// is record a new record?
public function is_new() {
return $this->is_new;
}
// is record a modified record?
public function is_modified() {
return $this->is_modified;
}
// internal use
// a table name
private function table() {
if (is_null($this->tablename)) {
return $this->pluralize(get_class($this));
} else {
return $this->tablename;
}
}
// ArrayAccess methods
public function offsetSet($offset, $value) {
$this->attributes[$offset] = $value;
$this->is_modified = true;
}
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;
}
// Iterator methods
public function rewind() {
reset($this->attributes);
}
public function current() {
return current($this->attributes);
}
public function key() {
return key($this->attributes);
}
public function next() {
next($this->attributes);
}
public function valid() {
return current($this->attributes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment