Skip to content

Instantly share code, notes, and snippets.

@timmahoney
Created January 12, 2012 17:42
Show Gist options
  • Save timmahoney/1601994 to your computer and use it in GitHub Desktop.
Save timmahoney/1601994 to your computer and use it in GitHub Desktop.
Set your Database table and table fields at the top, and screw creating models for interfacing with one table.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Generic CodeIgniter Database Model - One table version
*
* Set your Database table and table fields at the top,
* and screw creating models for interfacing with one table.
*
* Disclaimer: I'll answer questions or whatever, but I'm creating
* this to speed my own development up. I hope it helps, but it's not supported.
*
* SCHEMA:
* Create a table
* - with an "id" column that's an auto-increment primary key.
* - Some custom fields in here to be whatever you want
* - with a "created_datetime" column that's format DATETIME
* - with a "modified_datetime" column that's format DATETIME
*
* STARTER TABLE (My)SQL:
* CREATE TABLE IF NOT EXISTS YOUR_TABLE_NAME ( id INTEGER(11) AUTO_INCREMENT PRIMARY KEY, created_datetime DATETIME, modified_datetime DATETIME );
* @TODO Some way to manage joins?
*
*/
class YOUR_NAME_model extends CI_Model {
private $table_name = 'YOUR_TABLE_NAME';
private $field_keys = array("YOUR_FIELD_NAMES");
function __construct() {
parent::__construct();
}
function create_record($params=null) {
$params = $this->_check_array_keys( $params, $this->field_keys );
foreach($params as $key => $value){
$this->db->set( $key, $value );
}
$this->db->set( 'created_datetime', "NOW()", FALSE );
$this->db->set( 'modified_datetime', "NOW()", FALSE );
$this->db->insert( $this->table_name );
return $this->db->insert_id();
}
function retrieve_record($id=null, $where=null) {
if ( isset($where["key"]) ) { $this->db->where( $where["key"], $where["value"] ); }
elseif ( $where ) {
foreach($where as $where_item){
$this->db->where( $where_item["key"], $where_item["value"] );
}
}
$query = $this->db->get_where($this->table_name, array('id' => $id), 1);
return $query->row();
}
function update_record($params=null, $id=null) {
$params["modified_datetime"] = "NOW()";
$this->db->where('id', $id);
$this->db->update( $this->table_name, $params );
return $this->db->affected_rows();
}
function delete_record($id=null) {
$this->db->delete($this->table_name, array('id' => $id));
return $this->db->affected_rows();
}
function list_records($offset=0, $limit=20, $where=null){
if ( isset($where["key"]) ) { $this->db->where( $where["key"], $where["value"] ); }
elseif ( $where ) {
foreach($where as $where_item){
$this->db->where( $where_item["key"], $where_item["value"] );
}
}
$query = $this->db->get( $this->table_name, $limit, $offset);
return $query->result();
}
function count_records($where) {
$this->db->where( $where["key"], $where["value"] );
$this->db->from( $this->table_name );
return $this->db->count_all_results();
}
private function _check_array_keys( $params, $validator ) {
foreach( $params as $key=>$val ){
if( ! in_array ( $key, $validator ) ){
unset( $params[$key] );
}
}
return $params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment