Why Codeigniter Base Models Rock

codeigniter base crud modelsThere are a plethora of reasons to use a good base model (also called CRUD Model, MY-Model) for all your CRUD operations in a CodeIgniter (or any) application. Amongst many, a base model will boostrap all models it extends, keep your application as “DRY” as possible and speed up general development. Codeigniter has a pretty neat active record implementation, but you do tend to repeat a lot of the boring database stuff when writing models. In my opinion, you’d be insane not to use a good base model. There are many out there, but I use the amazing base model from Jamie Rumblelow (it deserves and SEO Link before you ask!).

Moving on, I was recently asked to update a freelance project I completed a while ago. The client wanted to display a record created on and record updated on for literally every item in their system. This potentially could run into a lot of work, as there are 17 models alone, each with at least 7 methods in each. Now, I absolutely hate repetitive tasks, as I’m sure any web developr does.

So after writing a quick, sql script to added two new fields (added_on and updated_on) to all database tables I moved on Codeigniter. Firstly, I was in luck, as I had used a base model and had a single entry point for all database interactions, this cut my work down massively before I’d even written any extra PHP.

One option for the task at hand may be to manually trawl through all the base model functions (there are quite a few) and add a new item to the data array. So say for the “insert” method I would have added the following before the active record code is run:

$data['added_on'] = $row['updated_on'] = date('Y-m-d H:i:s');

.. and for say the update function:

$data['updated_on'] = date('Y-m-d H:i:s');

Again, this is a bit time consuming. Jamie’s base model has callback support, meaning I can specify a function tcall (inline with all the MVC nazis out there, keeping such functionality within the model). Using this, I can simply populate the public $before_create and $before_update class variables:

public $before_create = array( '_create_timestamp' );
public $before_update = array( '_update_timestamp' );

.. I can now just add two very basic functions to the model:

protected function _create_timestamp($data)
{
   $data['added_on'] = $data['updated_on'] = date('Y-m-d H:i:s');
   return $data;
}

protected function _update_timestamp($data)
{
   $data['updated_on'] = date('Y-m-d H:i:s');
   return $data;
}

That’s it. Now, whenever I made an add or update call using the base model, the date fields will get populated accordingly.

I’ll be writing a post next about validation with Jamie’s base model – as CodeIgniter does encourage this within the the controller, which technically is MVC like – data validation should be in the model (you’ll notice two protected variables in his class, $validate and $skip_validation).

EDIT: in retrospect, I’ve have edited the model directly, which isn’t great practice as it may require an update at some point. It may have been better to specify the $before_create and $before_update variables within MY_Controller and place the methods there too. It would do the same thing, but avoid editing the model directly. Still, the whole update took less than 15 minutes, which is the whole point of using base models I guess :)

6 thoughts on “Why Codeigniter Base Models Rock

  1. Pingback: Validate Data within A Model using CodeIgniter & MY_Model

  2. Patrick

    Since i have a few tweaks that i always want over what the base MY_Model does, I actually just rename his to JR_Model, and put it in the core directory.

    My model does this:
    require_once(‘JR_Model.php’);
    class MY_Model extends JR_Model

    So, now I can do a single change there, like your updated_on stuff, and have it replicated automagically, and if Jamie changes his core model, to update my setup I just have to swap in the new file with the changed name, and no code editing of my own version.

    Reply
    1. Rob Allport Post author

      That’s a really good idea! I still votew for having the updated on and created at for each action in Jamie’s model by default – or at least having an option to turn it on and off :)

      Reply
  3. Pingback: Passing Arguments as an Associative Array to a Function - Web Design & Development Talk

  4. Business Facebook Page iwebxpert.com

    Thanks for sharing this fantastic information. Been using codeigniter lately and I would really say that it’s one of the great choice I’ve made.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>