Skip to content

Instantly share code, notes, and snippets.

@devyfriend
Created February 2, 2013 13:52
Show Gist options
  • Save devyfriend/4697463 to your computer and use it in GitHub Desktop.
Save devyfriend/4697463 to your computer and use it in GitHub Desktop.
Codeigniter MY_Model on core folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Model extends CI_Model {
public $error_string, $new_id;
function __construct()
{
parent::__construct();
}
/*
| -------------------------------------------------------------------------
| Generic functions
| -------------------------------------------------------------------------
*/
function Update($table, $primary_key, $primary_value, $data, $form_set=array())
{
$this->db->where($primary_key, $primary_value);
if(! empty($form_set))
{
foreach($form_set as $key=>$val)
{
$this->db->set($key,$val,false);
}
}
if ($this->db->update($table, $data))
{
return TRUE;
}else{
$this->error_string = mysql_error();
return FALSE;
}
}
function Insert($table, $form_data, $form_set=array())
{
if(! empty($form_set))
{
foreach($form_set as $key=>$val)
{
$this->db->set($key,$val,false);
}
}
if ($this->db->insert($table, $form_data))
{
$this->new_id = $this->db->insert_id();
return TRUE;
}else{
$this->error_string = mysql_error();
return FALSE;
}
}
function Delete($table, $primary_key, $primary_value, $where = false)
{
if ($this->db->delete($table, array($primary_key => $primary_value)))
{
return TRUE;
}else{
$this->error_string = mysql_error();
return FALSE;
}
}
function Select($table,$param = array())
{
if(isset($param['select']))
{
$this->db->select($param['select']);
}
if(isset($param['where']))
{
$this->db->where($param['where']);
}
if(isset($param['order']))
{
$this->db->order_by($param['order']['name'],$param['order']['type']);
}
if(isset($param['limit']))
{
if(isset($param['offset']))
{
$this->db->limit($param['limit'],$param['offset']);
}else{
$this->db->limit($param['limit']);
}
}
if(isset($param['like']))
{
foreach($param['like'] as $key=>$val)
{
$this->db->like($key,$val);
}
}
$res = $this->db->get($table);
//test($this->db->getSQL()); //exit;
if($res->num_rows() > 0)
{
return $res->result_array();
}else{
return false;
}
}
function SelectOne($table,$param = array())
{
$param['limit'] = 1;
$data = $this->Select($table,$param);
if(! empty($data)) return $data[0];
return false;
}
function returnQuery($sql)
{
$res = $this->db->query($sql);
if($res->num_rows() > 0)
{
return $res->result_array();
}else{
return false;
}
}
function doQuery($sql)
{
return $this->db->query($sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment