-
-
Save karlgray/81e4fc528bce6997113e75943291145f to your computer and use it in GitHub Desktop.
My base model class for my CodeIgniter-based cms.
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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* My Model | |
* | |
* Used as a base class for all admin or app related model functions. | |
* | |
* @author Alex Garcia | |
* @since 0.1 | |
* @version 1 | |
*/ | |
class MY_Model extends CI_Model{ | |
/** | |
* An array of fields defined in the database table. Do not include the id | |
* field which is a primary key. | |
* | |
* Usage: $this->db_fields = array('column_name','column_name2'); | |
* | |
* @var array | |
*/ | |
public $db_fields; | |
//-------------------------------------------------------------------------- | |
/** | |
* The name of the database table we want to work with. | |
* | |
* Usage: $this->db_table = 'name_of_table'; | |
* | |
* @var string | |
*/ | |
public $db_table; | |
//-------------------------------------------------------------------------- | |
/** | |
* An array of models that the current model requires. | |
* $this->db_requires = array('module_name'=>array('model_name','model_name)); | |
* | |
* @var array | |
*/ | |
public $db_requires; | |
/** | |
* The name of the module we are working with. | |
* | |
* @var string | |
*/ | |
public $module; | |
//-------------------------------------------------------------------------- | |
/** | |
* The name of the current method. | |
* | |
* @var string | |
*/ | |
public $method; | |
//-------------------------------------------------------------------------- | |
/** | |
* The id segment from the uri. | |
* | |
* @var mixed | |
*/ | |
public $id; | |
//-------------------------------------------------------------------------- | |
/** | |
* The constructor sets the fields property to a an empty array. | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->db_fields = array(); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Finds a set of records. | |
* | |
* If an id is present it will return that single record as an array. | |
* If the orderby array argument is present it will order the result based on | |
* the first and second indexes in the array. | |
* | |
* Usage: $this->model_name->find(); | |
* Usage: $this->model_name->find(1); | |
* Usage: $this->model->name->find(NULL, array('column_name','asc')) or desc. | |
* Usage: $this->model->name->find(NULL, array('column_name','asc'), 'id, first_name, last_name') | |
* | |
* @param int $id An optional id to return a single record. | |
* @param array $orderby An array with column and sort direction. array('column'=>'asc') | |
* @param string $select A string with the columns to select. 'id, first_name, last_name' | |
* @return array | |
*/ | |
public function find($id = NULL, $orderby = NULL, $select = NULL) | |
{ | |
if ( $id == NULL ) | |
{ | |
if ( $orderby != NULL ) | |
{ | |
$this->db->order_by($orderby[0], $orderby[1]); | |
} | |
if ($select != NULL) | |
{ | |
$this->db->select($select); | |
} | |
$result = $this->db->get($this->db_table); | |
if ($result->num_rows() > 0) | |
{ | |
return $result->result_array(); | |
} | |
else | |
{ | |
return array(); | |
} | |
} | |
else | |
{ | |
if ($select != NULL) | |
{ | |
$this->db->select($select); | |
} | |
$this->db->where('id',$id); | |
$result = $this->db->get($this->db_table); | |
if ($result->num_rows() > 0) | |
{ | |
return $result->row_array(); | |
} | |
else | |
{ | |
return array(); | |
} | |
} | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Finds a set of record for pagination. | |
* | |
* @param int $limit The number of records to return. | |
* @param int $offset Where to start the next set of records. | |
* @param array $orderby An array containing the column and direction to order the results by. | |
* @param string $select A string of column names to select. | |
*/ | |
public function find_count($limit = NULL, $offset = 0, $orderby = NULL, $select = NULL) | |
{ | |
$total_rows = $this->count(); | |
if ($limit == NULL) $limit = app('app_total_records'); | |
if ($orderby != NULL) $this->db->order_by($orderby[0], $orderby[1]); | |
if ($select != NULL) $this->db->select($select); | |
$result = $this->db->get($this->db_table, $limit, $offset); | |
return array('total_rows'=>$total_rows,'result'=>$result->result_array()); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Finds a set of records where the condition in column name and column | |
* value is true. | |
* | |
* Usage: $this->model_name->find_where('status','published'); | |
* Usage: $this->model_name->find_where('status !=','archived'); | |
* | |
* @param string $column_name The name of the column for our condtion. | |
* @param string $column_value The value of the column for our condition. | |
* @return array | |
*/ | |
public function find_where($column_name, $column_value) | |
{ | |
$this->db->where($column_name,$column_value); | |
$result = $this->db->get($this->db_table); | |
return $result->result_array(); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Saves a record to the database either as an insert or update. | |
* | |
* This is a massive function that also handles our workflow. Below is an | |
* explanation of how the workflow ties into this function. | |
* | |
* @param array $data An array of data that we want to save. | |
* @param int $id The primary key of a record we are updating. | |
* @return boolean | |
*/ | |
public function save($data = NULL, $id = NULL) | |
{ | |
if (! is_null($data)) | |
{ | |
$data = array_merge($this->prep_form_data('save'), $data); | |
} | |
else | |
{ | |
$data = $this->prep_form_data('save'); | |
} | |
if ($id == NULL) | |
{ | |
$result = $this->db->insert($this->db_table, $data); | |
return $result; | |
} | |
else | |
{ | |
$this->db->where('id', $id); | |
$result = $this->db->update($this->db_table,$data); | |
return $result; | |
} | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Saves a core record to the database either as an insert or update. | |
* | |
* This is a massive function that also handles our workflow. Below is an | |
* explanation of how the workflow ties into this function. | |
* | |
* @param array $data An array of data that we want to save. | |
* @param int $id The primary key of a record we are updating. | |
* @return boolean | |
*/ | |
public function save_direct($data, $id = NULL) | |
{ | |
if ($id == NULL) | |
{ | |
$result = $this->db->insert($this->db_table, $data); | |
return $result; | |
} | |
else | |
{ | |
$this->db->where('id',$id); | |
$result = $this->db->update($this->db_table,$data); | |
return $result; | |
} | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Deletes a record from the database. | |
* | |
* @param int $id The id of the record we want to delete. | |
* @return boolean | |
*/ | |
public function delete($id) | |
{ | |
$this->db->where('id', $id); | |
return $this->db->delete($this->db_table); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Returns the count of records for a table. We can filter that result | |
* by providing a column and value to create a condition. | |
* | |
* @param string $column The table column. | |
* @param string $value The table value. | |
* @return int The number of records. | |
*/ | |
public function count($column = NULL, $value = NULL) | |
{ | |
if($column != NULL AND $value != NULL) | |
{ | |
$this->db->where($column,$value); | |
} | |
$result = $this->db->get($this->db_table); | |
return $result->num_rows(); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Returns the count of records for a table. We can filter that result | |
* by providing a column and value to create a condition. | |
* | |
* @param string $column The table column. | |
* @param string $value The table value. | |
* @return int The number of records. | |
*/ | |
public function count_where($conditions = array()) | |
{ | |
foreach($conditions as $column=>$value) | |
{ | |
$this->db->where($column,$value); | |
} | |
$result = $this->db->get($this->db_table); | |
return $result->num_rows(); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Provides a way to perform searches of data. | |
* | |
* @param string $query A string of words seperated by commas. | |
* @param array $fields An array of fields to search. | |
* @param string $table The name of the table to search in. | |
* @return array | |
*/ | |
public function search($query, $fields = NULL, $table = NULL) | |
{ | |
$query = explode(',',$query); | |
if ($table == NULL) | |
{ | |
$table = $this->db_table; | |
} | |
if ($fields == NULL) | |
{ | |
$fields = $this->db_fields; | |
} | |
foreach($query as $term) | |
{ | |
foreach($fields as $field) | |
{ | |
//$this->db->select($field); | |
$this->db->or_like($field, $term); | |
} | |
$result[$term] = $this->db->get($table)->result_array(); | |
} | |
return $result; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Exports the current table into csv. | |
* | |
* @return void | |
*/ | |
public function export() | |
{ | |
$this->load->helper(array('file','download')); | |
$this->load->dbutil(); | |
$result = $this->db->get($this->db_table); | |
$data = $this->dbutil->csv_from_result($result); | |
$ts = time(); | |
force_download('log_'. $ts .'.csv', $data); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Preps our fields for our views and/or for our save method. This relies | |
* on the db_fields being defined in the model class for each model. | |
* | |
* @param string $action The default is create. Can also be save or update. | |
* @param array $model Passed to function like: $this->name_of_model->find($id) | |
* @param array $exclude An array of additional fields to exclude. | |
* @return array $view_data An array of fields prepped as empty or with data from our form. | |
*/ | |
public function prep_form_data($action = 'create', $model = NULL, $exclude = array()) | |
{ | |
if ($action == 'create') | |
{ | |
foreach($this->db_fields as $field) | |
{ | |
$view_data[$field] = ''; | |
} | |
} | |
elseif($action == 'save') | |
{ | |
foreach($this->db_fields as $field) | |
{ | |
if ( ! in_array($field, $exclude)) | |
{ | |
if (isset($field)) | |
{ | |
$view_data[$field] = $this->input->post($field); | |
} | |
else | |
{ | |
$view_data[$field] = ''; | |
} | |
} | |
} | |
} | |
elseif($action == 'update') | |
{ | |
foreach($this->db_fields as $field) | |
{ | |
if (isset($model[$field])) | |
{ | |
$view_data[$field] = $model[$field]; | |
} | |
else | |
{ | |
$view_data[$field] = ''; | |
} | |
} | |
} | |
return $view_data; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Finds a set of records for our form. | |
* | |
* If an id is present it will return that single record as an array. | |
* If the order by array argument is present it will order the result based on | |
* the first and second indexes in the array. | |
* | |
* Usage: $this->model_name->find_form(); | |
* Usage: $this->model_name->find_form((array) $data); | |
* Usage: $this->model->name->find_form((array) $data, 1); | |
* | |
* @param array $merge_data An array of additional data to merge with our db result to pass to our form. | |
* @param int $id An optional id to return a single record. | |
* @return array | |
*/ | |
public function find_form($merge_data = NULL, $id = NULL) | |
{ | |
if ($id == NULL) | |
{ | |
foreach($this->db_fields as $field) | |
{ | |
$view_data[$field] = ''; | |
} | |
} | |
else | |
{ | |
$this->db->where('id',$id); | |
$result = $this->db->get($this->db_table); | |
if ($result->num_rows() > 0) | |
{ | |
$result = $result->row(); | |
foreach($this->db_fields as $field) | |
{ | |
$view_data[$field] = $result->$field; | |
} | |
} | |
else | |
{ | |
return array(); | |
} | |
} | |
if ($merge_data != NULL) | |
{ | |
$view_data = array_merge($view_data, $merge_data); | |
} | |
return $view_data; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Clears the database cache. | |
* | |
* @return void | |
*/ | |
public function clear_cache() | |
{ | |
$this->db->cache_delete($this->module, $this->db_table); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment