Welcome Guest, Not a member yet? Register   Sign In
CRUD with Ci 1.7
#24

[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);
    }
}


Messages In This Thread
CRUD with Ci 1.7 - by El Forum - 01-29-2009, 07:35 PM
CRUD with Ci 1.7 - by El Forum - 01-30-2009, 01:15 AM
CRUD with Ci 1.7 - by El Forum - 01-30-2009, 05:21 AM
CRUD with Ci 1.7 - by El Forum - 02-11-2009, 07:56 PM
CRUD with Ci 1.7 - by El Forum - 02-12-2009, 12:35 AM
CRUD with Ci 1.7 - by El Forum - 02-13-2009, 02:15 AM
CRUD with Ci 1.7 - by El Forum - 02-13-2009, 02:55 AM
CRUD with Ci 1.7 - by El Forum - 02-13-2009, 11:32 AM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 04:46 AM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 04:58 AM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 11:27 AM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 12:00 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 12:51 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 12:59 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 01:02 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 01:06 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 01:14 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 01:45 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 02:55 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 02:58 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 03:06 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 03:09 PM
CRUD with Ci 1.7 - by El Forum - 02-14-2009, 04:52 PM
CRUD with Ci 1.7 - by El Forum - 02-24-2009, 08:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB