[eluser]cray[/eluser]
Hi,
I have been using codecrafter when I start using CI, but later I built this 'base_model' for simple CRUD model (based on what coderafter made and use active record to do the task).
To use it we just have to specify the "table_name" (required) and optional "select" and "join" parameters if we need it to be more specific. Here is the way I initialize it:
Code:
function __construct()
{
parent::Controller();
$this->load->model('BASE_MODEL', 'invoice_model');
$this->invoice_model->table_name = 'invoice';
$this->invoice_model->table_select = 'invoice.*, customer.company';
$this->invoice_model->table_join = array('customer', 'customer.id = invoice.cust_id', 'INNER');
$this->load->model('BASE_MODEL', 'invoice_item_model');
$this->invoice_item_model->table_name = 'invoice_item';
}
and here is the way to use the model:
--------------------------------------
a.) $count = $this->invoice_model->record_count();
b.) $blank = $this->invoice_model->blank();
c.) $this->invoice_model->add($data)
d.) $this->invoice_model->update($id, $data)
e.) $this->invoice_model->delete($id)
f.) $data['invoice'] = $this->invoice_model->findAll();
$data['invoice'] = $this->invoice_model->findById($id);
$data['invoice'] = $this->invoice_model->findByFilter("id = $id");
Hope it could be usefull...
.......
Code:
/**
* Module : BASE_MODEL.PHP
* -------------------------------------
* Descr : General purpose 'Base Model' basic CRUD functions
* Author : SG4 / Surya Arifien / 2008
*
* Usage : $this->load->model('BASE_MODEL', 'my_model');
* $this->my_model->table_name = 'my_table';
*/
class Base_Model extends Model {
var $table_name; // table name (required)
var $table_select = NULL; // default select, override when you needed to select
var $table_join = NULL; // join array parameters container
function __construct()
{
parent::Model();
if (!isset($this->CI->db)) $this->load->database();
}
// calculates record count for current table
function record_count()
{
return $this->db->count_all($this->table_name);
}
// creates a blank array with field as the keys
function blank()
{
$fields = $this->db->list_fields($this->table_name);
foreach ($fields as $field) {
$data[$field] = '';
}
return $data;
}
//---------------- Retrieve Functions---------------
function findAll($start = NULL, $count = NULL)
{
return $this->find(NULL, $start, $count);
}
function findById($id)
{
$result = $this->find($this->table_name.".id = $id");
return $result[0];
}
function findByFilter($filter_rules, $start = NULL, $count = NULL)
{
return $this->find($filter_rules, $start, $count);
}
function find($filters = NULL, $start = NULL, $count = NULL)
{
if (!is_null($filters)) $this->db->where($filters);
$this->db->limit($count, $start);
$this->db->from($this->table_name);
if (!empty($this->table_select))
$this->db->select($this->table_select, FALSE); // un-protected select
if (!empty($this->table_join))
$this->db->join($this->table_join[0], $this->table_join[1], $this->table_join[2]);
$query = $this->db->get();
// echo $this->db->last_query(); die;
return $query->result_array();
}
//----------- Create(Add), Update & Delete ----------
function add($data)
{
return $this->db->insert($this->table_name, $data);
}
function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table_name, $data);
}
function delete($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table_name);
}
}