Skip to content

Instantly share code, notes, and snippets.

@hamstar
Created February 9, 2012 04:59
Show Gist options
  • Select an option

  • Save hamstar/1777436 to your computer and use it in GitHub Desktop.

Select an option

Save hamstar/1777436 to your computer and use it in GitHub Desktop.
SuperModel/Model setup
<?php
class EventNotFoundException extends Exception{};
class Event extends SuperModel {
protected $TABLE = 'event';
protected $db_fields;
protected $id;
protected $type;
protected $message;
protected $raw;
protected $extra_data;
protected $trace;
protected $created;
protected $project_id;
protected $status;
protected $project;
public function __clone() {
unset( $this->count_only );
}
// <editor-fold defaultstate="collapsed" desc=" Factory Methods ">
public function find($id) {
$query = $this->db->where('id', $id)->get($this->TABLE);
if ($query->num_rows() == 0)
throw new EventNotFoundException();
$row = $query->row();
return $this->create($row);
}
public function forProject($project) {
if (is_numeric($project)) {
$id = $project;
} else if (is_a($project, 'Project')) {
$id = $project->get_id();
} else {
throw new InvalidArgumentException("Invalid project given: $project");
}
$this->db->where('project_id', $id);
if ( isset($this->count_only) ) {
unset( $this->count_only );
return $this->db->count_all( $this->TABLE );
}
$query = $this->db->get($this->TABLE);
if ($query->num_rows() == 0)
return array();
$events = array();
foreach ($query->result() as $row)
$events[] = $this->create($row);
return $events;
}
function count() {
$this->count_only = true;
return $this;
}
// </editor-fold>
public function parse() {
return $this;
}
// <editor-fold defaultstate="collapsed" desc=" Setters ">
public function set_raw($raw) {
$this->raw = $raw;
return $this;
}
public function set_status($status) {
$this->status = $status;
return $this;
}
public function set_type($type) {
$this->type = $type;
return $this;
}
public function set_extra_data($extra_data) {
$this->extra_data = $extra_data;
return $this;
}
public function set_trace($trace) {
$this->trace = $trace;
return $this;
}
public function set_message($message) {
$this->message = $message;
return $this;
}
public function set_project(Project $project) {
$this->project_id = $project->get_id();
$this->project = $project;
return $this;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc=" Getters ">
public function get_message() {
if (is_null($this->message))
return "Unknown";
return $this->message;
}
public function get_trace() {
return $this->trace;
}
public function get_extra_data() {
return $this->extra_data;
}
public function get_type() {
if (is_null($this->type))
return "Unknown";
return $this->type;
}
public function get_raw() {
return $this->raw;
}
public function get_id() {
return $this->id;
}
public function get_status() {
if (is_null($this->status))
return "Unknown";
return $this->status;
}
public function get_project_id() {
return $this->project_id;
}
public function get_project() {
return $this->project;
}
public function decode_extra_data() {
if ( is_null( $this->extra_data ) )
return NULL;
if ( !is_null($json = json_decode($this->extra_data)))
return $json;
}
// </editor-fold>
}
<?php
class SuperModel {
private $db;
function __construct() {
$this->db = new DB(); // your db class (in this case codeigniter db class)
}
function save() {
if ( method_exists( $this, 'pre_save') )
$this->pre_save();
if ( is_null( $this->db_fields ) ) {
$this->db_fields = $this->db->list_fields( $this->TABLE );
$new = TRUE;
}
$row = new StdClass;
foreach ( $this->db_fields as $field )
$row->$field = $this->$field;
if ( $new ) {
$this->db->insert( $this->TABLE, $row );
} else {
$this->db->where('id', $this->id)->update( $this->TABLE, $row );
}
return $this;
}
function populate($row) {
$this->reset();
if ( is_null( $row ) )
return $this;
$this->db_fields = get_object_vars($row);
foreach ( $this->db_fields as $field => $v)
$this->$field = $row->$field;
return $this;
}
function create( $row=null ) {
$model = clone $this;
$model->populate( $row );
return $model;
}
function find_all() {
$models = array();
$query = $this->db->get( $this->TABLE );
if ( $query->num_rows() == 0 )
return array();
foreach ( $query->result() as $row )
$models[] = $this->create( $row );
return $models;
}
function reset() {
$fields = ( get_object_vars( $this ) );
unset( $fields['TABLE']);
foreach ( $fields as $field => $v )
$this->$field = null;
return $this;
}
function get_created($fmt='jS F Y') {
return date( $fmt, strtotime( $this->created ));
}
function get_id() {
return $this->id;
}
}
<?php
$event_factory = new Event();
$event = $event_factory->find( 44 ); // gets one event with the id of 44
$events = $event_factory->find_all(); // finds all the events in the table - returns array
$event = $event_factory->create(); // creates a blank event
// You should be able to even use the generated events as factories (I've not tested this though)
$new_event = $event->create();
// Then you can save
$event->set_message( "a message" )->save();
// And friendly interfaces are fun
$event->create()->set_message("a message")->set_trace("a trace")->save();
echo $event->find( 4 )->set_message("blah")->save()->get_message();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment