CodeIgniter Setup – Part 3

Here I will explain how to setup Base Models in CodeIgniter.

This follows on from Part 2 so I recommend that you read this first before continuing.

Full working example of all 3 parts available on GitHub:

Why would you want to use Base Models?

Many of the calls that you use accessing a database are the same. Get by Id, get all, get by column etc.

By using Base Models you don’t need to repeat these calls inside each model, but only in the Base Model.

Your custom models are now only for custom queries like joining 2 tables together.

edit core/application/config/autoload.php

$autoload['libraries'] = array('database','session'); // calls the database library on all pages

import into your database db_schema/example.sql

This is just to get my example working.

edit core/application/config/database.php

add in your database access information

create core/application/core/My_Model.php

You can create as many functions here that you will use regularly like save, edit, delete etc.

create core/application/models/example.php

<?php class Example extends MY_Model {
protected $tableName = 'example';
protected $primaryKey = 'id';

edit core/application/controllers/cms.php

Example of the view

So that is everything you need to setup Base Models in your CodeIgniter build.

Full working example available on GitHub:

Leave a Reply

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