PHP Classes
elePHPant
Icontem

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    
Last Updated Ratings Unique User Downloads Download Rankings
2015-07-22 (4 years ago) RSS 2.0 feedNot enough user ratingsTotal: 309 All time: 7,177 This week: 195Up
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..

Name: Simo <contact>
Classes: 3 packages by
Country: Morocco Morocco
Age: ???
All time rank: 20163 in Morocco Morocco
Week rank: 324 Up1 in Morocco Morocco Up
Innovation award
Innovation award
Nominee: 1x

 

Details

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  
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  /  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  
 100%
Total:309
This week:0
All time:7,177
This week:195Up
User Comments (1)
A change that could be made is for multiple connections to th...
4 years ago (Renato Moreira de Araujo)
65%StarStarStarStar