This is an extended Model class to use in CodeIgniter applications that provides a base CRUD pattern for your models. It gives you the core CRUD functionality, as well as support for before_create and after_create callbacks and some clever table name guessing.


Drag the MY_Model.php file into your application/libraries folder. CodeIgniter will load and initialise this class automatically for you. Then all you have to do is make sure all your models extend from MY_Model and all the functionality will be baked into your models automatically!

Naming Conventions

This class will try to guess the name of the table to use, by guessing the plural of the class name. If the table name isn't the plural and you need to set it to something else, just declare the $table instance variable and set it to the table name. Some of the CRUD functions also assume that your primary key ID column is called 'id'. You can overwrite this functionality by setting the $primary_key instance variable.


Validation support has been added. To use this set the protected $validate variable to the rules array. It uses the built in Form Validation library. So all rules are the same. You also use the helper functions in your views like normal. Here is an example:

class User_model extends MY_Model
    protected $validate = array(
        array('field'   => 'username',
              'label'   => 'Username',
            'rules'   => 'required'),
        array('field'   => 'email',
              'label'   => 'Email',
             'rules'   => 'required|valid_email')
    // Other things here

Upcoming Features

  • Support for validations
  • Before and after update callbacks
  • Better table name guessing
  • Better support for associations and JOINs

Version History

  • 1.1.1 A limit() method, to allow limiting result sets
  • 1.1.0 Two new methods, count_by() and count_all(), made use of some ActiveRecord methods and optimisations
  • 1.0.5 Added support for custom primary keys
  • 1.0.0 First release