I have made a model library for codeigniter. It is based on the article http://maestric.com/doc/php/codeigniter_models
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Model extends Model { var $table = ""; function My_Model() { parent::Model(); $this->load->database(); } function find($type = 'all', $options = array()) { if ($type == 'first') { $this->db->limit(1); } if(array_key_exists('fields', $options)) { $this->db->select(implode(",", $options['fields'])); } if(array_key_exists('where', $options)) { foreach ($option['where'] as $key=>$value) { $this->db->where($key, $value); } } if(array_key_exists('order', $options)) { foreach ($option['where'] as $key=>$value) { $this->db->order_by($key, $value); } } if(array_key_exists('limit', $options)) { $limit = $options['limit']; if(array_key_exists('limit', $limit) && !array_key_exists('offset', $limit)) { $this->db->limit($limit['limit']); } if(array_key_exists('limit', $limit) && array_key_exists('offset', $limit)) { $this->db->limit($limit['limit'], $limit['offset']); } } $query = $this->db->get($this->table); $result = $query->result_array(); return (count($result) > 0 ? $result[0] : NULL); } function find_first($options) { return $this->find('first', $options); } function find_all($options) { return $this->find('all', $options); } function find_id($id) { if ($id == NULL) { return NULL; } return $this->find('first', array('where'=>array('id'=>$id))); } function insert($data) { $this->db->insert($this->table, $data); return $this->db->insert_id(); } function update($id, $data) { $this->db->where('id', $id); $this->db->update($this->table, $data); } function delete($id) { if ($id != NULL) { $this->db->where('id', $id); $this->db->delete($this->table); } } }