Created
August 29, 2011 02:20
-
-
Save airways/1177630 to your computer and use it in GitHub Desktop.
CodeIgniter base model classes
This file contains hidden or 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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* SUSHI_Model provides a base model class that interacts with SUSHI_ModelRow to | |
* make saving and loading data easier. | |
* | |
* @package default | |
* @author Isaac Raway | |
**/ | |
class SUSHI_Model extends CI_Model | |
{ | |
public $table = ''; | |
public $singular = ''; | |
public $class = ''; | |
public $serialized = array(); | |
public function __construct($table = FALSE, $singular = FALSE, $class = FALSE, $serialized = FALSE) | |
{ | |
parent::__construct(); | |
/*$this->CI->db->cache_off(); | |
if($table) $this->table = $table; | |
if($singular) $this->singular = $singular; | |
if($class) $this->class = $class; | |
if($serialized) $this->serialized = $serialized;*/ | |
} | |
/** | |
* Wrapper around ImageRow constructor to hide class name from client code. | |
* | |
* @return ImageRow | |
* @author Isaac Raway | |
**/ | |
public function create() | |
{ | |
return new {$this->class}(); | |
} | |
} // END SUSHI_Model | |
/** | |
* SUSHI_ModelRowMeta provides a dumping ground for data that is not saved for a row but | |
* is needed to manage it's lifecycle. Basically we just use this instead of an array | |
* to keep the syntax cleaner. | |
* | |
* @package default | |
* @author Isaac Raway | |
**/ | |
class SUSHI_ModelRowMeta | |
{ | |
public $new = TRUE; | |
public $model = NULL; | |
} | |
/** | |
* SUSHI_ModelRow provides facilities for automatically interacting with model objects | |
* in an object oriented way (new record created with new ClassName(), save with | |
* $inst->save(), etc.) | |
* | |
* @package default | |
* @author Isaac Raway | |
**/ | |
class SUSHI_ModelRow | |
{ | |
public $meta = NULL; | |
public function __construct(&$row = FALSE) | |
{ | |
// The META field contains all non-data fields that are not saved or loaded from the datasource | |
$this->META = new SUSHI_ModelRowMeta(); | |
$this->CI = &get_instance(); | |
if($row) | |
{ | |
$this->META->new = FALSE; | |
foreach($row as $key => $value) | |
{ | |
if($key != 'CI' && $key != 'META') | |
{ | |
$this->$key = &$value; | |
} | |
} | |
} | |
} | |
public function dump() | |
{ | |
echo "<b>DUMP " . get_class($this) . "</b><br/>"; | |
echo "<i>Meta:</i><br/>"; | |
foreach($this->META as $key => $value) | |
{ | |
echo $key . "=" . (is_object($value) ? 'OBJECT: ' . get_class($value) : $value) . "<br/>"; | |
} | |
echo "<i>Fields:</i><br/>"; | |
foreach($this as $key => $value) | |
{ | |
if($key != 'CI' && $key != 'META') | |
{ | |
echo $key . "=" . (is_object($value) ? 'OBJECT: ' . get_class($value) : $value) . "<br/>"; | |
} | |
} | |
echo "<br/>"; | |
} | |
public function save() | |
{ | |
$id_field = $this->META->model->singular.'_id'; | |
$this->_serialize(); | |
if($this->META->new) | |
{ | |
// Create new record | |
$this->CI->db->insert($this->table, $data); | |
$insert_id = $this->CI->db->insert_id(); | |
$this->$id_field = $insert_id; | |
} else { | |
// Update existing record | |
$this->CI->db->where($id_field, $this->$id_field)->update($this->META->model->table, $this); | |
} | |
$this->_unserialize(); | |
} | |
public function delete() | |
{ | |
$id_field = $this->META->model->singular.'_id'; | |
return $query = $this->CI->db->where($id_field, $this->$id_field) | |
->delete($this->table); | |
} | |
private function _serialize() | |
{ | |
foreach($this->META['model']->serialized as $field) { | |
if(isset($this->$field)) { | |
$this->$field = serialize($this->$field); | |
} | |
} | |
} | |
private function _unserialize() | |
{ | |
foreach($this->META['model']->serialized as $field) { | |
if(isset($this->$field)) { | |
$this->$field = unserialize($this->$field); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment