My CodeIgniter Base Model
A friend of mine, Doug Linsmyer, introduced me to Shawn McCool’s basic CRUD model that he uses in CodeIgniter development and I immediately started using it in my CI projects. However, I couldn’t handle how much repeated code there was in my models so, with some help from my friends on Forrst, I turned it into a base model that I can use for all of my models. Now all I have to do to create a new model with my CRUD methods is extend them with MY_Model and declare a couple variables.
Setup
In order to get up and running with the base model just:
- Download it and drop it into your
application/core
folder. - Extend your models with
MY_Model
. - Set the
primary_table name
.
That’s all that is required, but there are a couple more options. Here’s a full example:
class Company_model extends MY_Model {
var $primary_table = 'companies';
var $validate_field_existence = TRUE;
var $fields = array(
'id',
'name',
'address',
'city',
'state',
'zipcode',
'phone',
'is_active',
'date_created',
'date_modified'
);
var $required_fields = array(
'name',
'address',
'city',
'state',
'zipcode',
'phone'
);
}
There are 5 options you can declare:
- primary_table (required): The name of the table the model should execute queries on.
- fields: An array of the fields in the database that the model has access to. If you don’t specify the fields here they will be pulled dynamically from the database and the query will be cached.
- required_fields: An array of fields that must be submitted any time a record is created or updated.
- models: Set an array of models to be loaded. This can also be set in a constructor, but it makes things a little easier.
- validate_field_existence: Validates that the fields you are trying to modify actually exist in the database. Only use this method for debugging, it is not fit for production code because of the number of queries it has to run
Usage
All of the commands accept an array of parameters where the column name is the key. When adding and updating a record you simply pass an array of fields you want to change with the values you want them set to:
$options = array(
'name' => $this->input->post('company_name'),
'address' => $this->input->post('company_address'),
'city' => $this->input->post('company_city'),
'state' => $this->input->post('company_state'),
'zipcode' => $this->input->post('company_zip'),
'phone' => $this->input->post('company_phone'),
'is_active' => $this->input->post('company_is_active')
);
$company = $this->company_model->add($options);
When selecting records you can use any field in the database as a filter:
$options = array(
'name' => $this->input->post('search_name'),
'zipcode' => $this->input->post('search_zipcode')
);
$company = $this->company_model->get($options);
That’s about all there is to it. The code is up on GitHub so please feel free to contribute and make this better. I know Jamie Rumbelow and Phil Sturgeon have a base model that provides some similar functionality with some additional methods, but I haven’t really used it much yet. This approach has worked well for me in my projects so I thought I would share it here, but I’m sure it can be drastically improved.
Source: