PHP Classes

CodeIgniter Extended Model: Extends CodeIgniter model to provide more features

Recommend this page to a friend!
  Info   View files Documentation   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 320 All time: 7,229 This week: 455Up
Version License PHP version Categories
codeigniter-model 1.0MIT/X Consortium ...5.3PHP 5, Databases, Libraries, Design P...


This class extends CodeIgniter model class to provide more features.

The MY_Model class extends CodeIgniter model class. Model classes should extend the MY_model to define properties of each model class like the table, primary key, fields, foreign key, relationships with other models like has_many, has_one, belongs_to, etc..

The class can perform ORM operations like get one or all objects of the model class or those that match a criteria, get paginated lists of objects, get objects defined by relationships, perform CRUD operations (list, add, update and delete objects), etc..

Picture of Simo
Name: Simo <contact>
Classes: 4 packages by
Country: Morocco Morocco
Age: ???
All time rank: 19143 in Morocco Morocco
Week rank: 225 Up2 in Morocco Morocco Up
Innovation award
Innovation award
Nominee: 2x


CodeIgniter Model Class

CodeIgniter Model is an extended class for CI_Model, it will help you to :

  • CRUD operations.
  • Auto-save for multilang tables.
  • Fetch data using associations like oneToMany, ManyToOne
  • Apply filters to results
  • Pagination...


CodeIgniter Versoin >= 2.x.x

Copy the file MY_Model.php to the application/core/ folder.


Create a Model that extends from MY_Model and set the variables :

protected $table = 'my_table';

protected $identifier = 'id_my_table';

protected $foreign_key = 'my_table_id';

protected $fields = array(


Assuming that we have 2 tables advertisement and category, each advertisement belongs to a category, so a category can have one or many advertisements, which means our tables may look like this :

NOTES : - The category table has multilang fields so we're gonna put them in category_lang table - {table}_lang must respect the recommended structure

----------------------- | category | ----------------------- | id_category (int) | | parent (int) | | created_at (datetime) | | updated_at (datetime) | ----------------------- ----------------------- | category_lang | ----------------------- | category_id (int) | | lang_id (int) | | title (varchar) | | description (varchar) | ----------------------- ----------------------- | advert | ----------------------- | id_advert (int) | | category_id (int) | | title (int) | | description (int) | | deleted (tinyInt) | | created_at (datetime) | | updated_at (datetime) | -----------------------

Fetch records

After that, you can call the Model within your controller for instance like following :

// Fetch all records
$records = $this->New_model->get_all();

// Return record with id=10
$record = $this->New_model->get(10);

Fetch records with criteria

// You can also add some criteria like following :
$criteria = array();
$criteria[] = array('title', 'like', 'test');
$criteria[] = array('id_my_table', '=', 10);

$result = $this->New_model->get_all($criteria);

Fetch record with association

$result = $this->Advert_model
                ->order_by('created_at', 'DESC')

$result = $this->Category_model

Fetch records with multiple associations

$result = $this->Advert_model
                ->order_by('created_at', 'DESC')

Fetch records with filter on the associations

$filter = array();
$filter[] = array('lang_id', '=', 2);
$result = $this->Advert_model
                ->with('category', $filter)
                ->order_by('created_at', 'DESC')

Fetch records with pagination

$page = 1;
$total_items_per_page = 100;
$records = $this->Advert_model
                ->limit($total_items_per_page, $page)
                ->order_by('created_at', 'DESC')

$total_records = $this->Advert_model->count_all_results();

Add new record

$data = array(
  'title' => 'New Advertisement',
  'description' => 'New Advertisement',
  'deleted' => 0,
  'category_id' => 23,

Update record

$id_record = 10;
$data = array(
  'title' => 'Update Advertisement',
  'description' => 'Update Advertisement',
  'deleted' => 0,
  'category_id' => 23,
$this->Advert_model->save($data, $id_record);

Delete record

$id_record = 10;

Check if a record exists

$exists = $this->Advert_model->exists('id_advert', 10);

// $exists = true : if the record exists, and false if not

// Check if an email address exists but the user id must NOT be mine 
$exclude_condition = array('id_user' => $this->logged_user->get_id());
$exists = $this->User_model->exists('email', '', $exclude_condition);

  Files folder image Files  
File Role Description
Files folder imageapplication (1 file, 2 directories)
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file Doc. Auxiliary data

  Files folder image Files  /  application  
File Role Description
Files folder imagecore (2 files)
Files folder imagemodels (2 files)
  Accessible without login HTML file index.html Data Documentation

  Files folder image Files  /  application  /  core  
File Role Description
  Accessible without login HTML file index.html Data Documentation
  Plain text file MY_Model.php Class Class source

  Files folder image Files  /  application  /  models  
File Role Description
  Plain text file Advert_model.php Class Example script
  Plain text file Category_model.php Class Example script

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:7,229
This week:455Up
User Comments (1)
A change that could be made is for multiple connections to th...
8 years ago (Renato Moreira de Araujo)