PHP Classes

CodeIgniter Extended Model: Extends CodeIgniter model to provide more features

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

Author

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
Innovation award
Innovation award
Nominee: 2x

Documentation

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...

Installation

CodeIgniter Versoin >= 2.x.x

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

Usage

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(
    'field_1',
    'field_2',
);

Example

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
$this->load->model('New_model');
$records = $this->New_model->get_all();
var_dump($records);

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

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);
                
var_dump($result);

Fetch record with association

$result = $this->Advert_model
                ->with('category')
                ->order_by('created_at', 'DESC')
                ->get_all($criteria);
                
var_dump($result);

$result = $this->Category_model
                ->with('advert')
                ->get_all();
                
var_dump($result);

Fetch records with multiple associations

$result = $this->Advert_model
                ->with('category')
                ->with('alias_2')
                ->with('alias_3')
                ->order_by('created_at', 'DESC')
                ->get_all($criteria);
                
var_dump($result);

Fetch records with filter on the associations

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

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')
                ->get_all($criteria);

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

Add new record

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

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;
$this->Advert_model->delete($id_record);

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', 'example@email.com', $exclude_condition);


  Files folder image Files (7)  
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 README.md Doc. Auxiliary data

  Files folder image Files (7)  /  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 (7)  /  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 (7)  /  application  /  models  
File Role Description
  Plain text file Advert_model.php Class Example script
  Plain text file Category_model.php Class Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:320
This week:0
All time:7,232
This week:455Up
User Comments (1)
A change that could be made is for multiple connections to th...
9 years ago (Renato Moreira de Araujo)
65%StarStarStarStar