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

[eluser]Xeoncross[/eluser]
Sorry, I wasn't clear in my post. I do use models - and only models - for data fetching and storing. I never use a controller or library for that stuff.

Code:
$this->blog_model->fetch_posts($user_id, $limit);

I meant that I don't think that there is really any better way to interface with the database than to use the awesome AR that is already in CI.

I don't think that you could put one more class between your models and the database AR to make it any easier.
#22

[eluser]Colin Williams[/eluser]
Gotcha, Xeoncross. Figured I was a bit off-base there. I don't think the goal of creating a more automated model would have to much to do with improving the interface to the database. But you can set up some conventions in your models that do expedite some repetitive tasks that are outside the realm of the database class.
#23

[eluser]Xeoncross[/eluser]
[quote author="Colin Williams" date="1234667354"]But you can set up some conventions in your models that do expedite some repetitive tasks that are outside the realm of the database class.[/quote]

For example, I usually use this code to keep from testing for valid results in all my models.

Code:
function __construct() {

    parent::Model();

    //Set the forum table names
    $this->_tables = array(
        'users' =>'users',
        'posts' => 'user_posts',
        'topics' => 'user_topics'
    );

}

/**
* Get the result object - or fail
* Helps with "DRY" code
* @return object
*/
protected function _get($t='users') {

    //If a name was given
    $result = $this->db->get($t ? $this->_tables[$t] : NULL);
    
    //If not found - return false
    if(!$result->num_rows()) {
        return FALSE;
    }
    return $result;
}


//Example 1
function fetch_users() {
    return $this->_get('users');
}
#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);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB