Skip to content

Instantly share code, notes, and snippets.

@nazieb
Last active December 19, 2015 16:39
Show Gist options
  • Select an option

  • Save nazieb/5985649 to your computer and use it in GitHub Desktop.

Select an option

Save nazieb/5985649 to your computer and use it in GitHub Desktop.
Base_Model for CodeIgniter. The idea is taken from Laravel's Eloquent ORM
<?php
/**
* Base Model to provide ORM-like CRUD operation wrapper
* Version: 1.1
*/
class Base_Model extends CI_Model {
protected $table = "";
protected $primary = "id";
protected $per_page = 20;
protected $order = "";
protected $default_filter = array();
function __construct()
{
parent::__construct();
$this->load->database();
}
/**
* Set default WHERE clause for each SELECT query
* @param $field string Field name
* @param $value string Value of the field
*/
function set_default_filter($field, $value)
{
$this->default_filter[$field] = $value;
}
/**
* Implement each $default_filter set above
* To be called in every method which generate SELECT query
* @access protected
* @return void
*/
protected function default_filter()
{
if(empty($this->default_filter)) return;
foreach ($this->default_filter as $field => $value)
$this->db->where($field, $value);
}
/**
* Run a query to database after implementing the default filter
* Return the query object to be processed further by its caller
* @access private
* @param int page The current pagination to get
* @return CodeIgniter query object
* @see this::default_filter
*/
private function query($page = 1)
{
$this->default_filter();
if(!empty($this->per_page))
{
$offset = ($page - 1) * $this->per_page;
if($offset < 0) $offset = 0;
$this->db->limit( $this->per_page, $offset );
}
if(!empty($this->order))
$this->db->order_by( $this->order );
else
$this->db->order_by( $this->primary, 'desc' );
return $this->db->get( $this->table );
}
function lists($page = 1)
{
$query = $this->query($page);
return $query->num_rows > 0 ? $query->result() : array();
}
function first()
{
$query = $this->query();
return $query->num_rows > 0 ? $query->row() : array();
}
/**
* Find a row using primary key
* @param id The "ID" to find. Uses column which defined in $primary property above
* @return stdClass|null
*/
function find($id)
{
$this->default_filter();
$query = $this->db->get_where( $this->table, array($this->primary => $id) );
return $query->num_rows == 1 ? $query->row() : null;
}
function count()
{
$this->default_filter();
return $this->db->get($this->table)->num_rows();
}
function save($data)
{
if(is_object($data)) $data = json_decode(json_encode($data), true);
if(!isset($data[ $this->primary ]) or empty($data[ $this->primary ]))
return $this->db->insert( $this->table, $data );
else
return $this->db->update( $this->table, $data, array($this->primary => $data[ $this->primary ]) );
}
function delete($id)
{
return $this->db->delete($this->table, array($this->primary => $id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment