Git Product home page Git Product logo

codeigniter-powerful-model's Introduction

CodeIgniter Powerful Model

A CodeIgniter extension to work with database tables with an easy and intuitive Object Oriented / Entity Framework approach.

Installation

Download the files from this repository and place them into the corresponding folders in your CodeIgniter project.
The add-in consists of a solid MY_Model implementation and also redefines the CI_Loader::model method. If you already have a MY_Loader extension, just copy the MY_Loader::model extended method within your extended class.

Usage

Let's make some examples with a classic cars table.

Table Schema

The best practice is to name your table in plural form (eg. cars), and name the primary key column as id.
There's no particular preferences for other column names.
Mind that you can still use other naming conventions for your tables. In this case, it's necessary to set this names by manually set the value of table, id_field and row_type properties in the constructor of the entity model definition (see next paragraph).

Define a New Entity Object

In the Models folder, create a new Cars_model.php file defining the Cars_model extending MY_Model class (use the plural for the entire table) and the Car_object extending Model_object class (use the singular for the single record).

Here the code in Models/Cars_model.php:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Cars_model extends MY_Model {
	
	public function __construct()
	{
		// If you use standard naming convention, this code can be omitted.
		/*$this->table = 'cars';
		$this->id_field = 'id';
		$this->row_type = 'Car_object';*/
		parent::__construct();
	}
}

class Car_object extends Model_object {
	
}

Load a Model

$this->load->model('Cars');
// or autoload in CI's application/config/autoload.php like this: $autoload['model'] = array('Cars');

Get All Records

$all_cars = $this->Cars->get_list();

Get Some Records

// Get some records (apply a filter in query)
$some_cars = $this->Cars->where('brand_id', 1)->get_list();

Get a Single Record

$a_car = $this->Cars->get(1);	// this is a "get by id"

Add a New Record

$new_car = $this->Cars->new_row();
$new_car->name = "Powerful Car";
$new_car->brand_id = 1;
$id = $new_car->save();	// this produces the insert command

Make Some Changes

$edit_car = $this->Cars->get($id);
$edit_car->name = "Change its name";
$edit_car->save();		// this produces the update command (only for the changed fields, the CI Powerful Model tracks object changes)

Automatic Join

$all_cars = $this->Cars->autojoin()->get_list();	// automatic LEFT join with the brands table
// uses the CI's inflector helper to transform <entity>_id to <entities>

Manual Join

$this->Cars->join('brands', 'cars.brand_id = brands.id', 'LEFT');
$all_cars = $this->Cars->get_list();

CodeIgniter's Query Builder

You can use all the CodeIgniter's Query Builder methods, allowing method chaining.

$some_cars = $this->Cars->where_in('brand_id', array(1, 2, 3))->like('name', "%Something")->order_by('name')->get_list();

Pagination

// (page 1, with 10 cars per page)
$cars_page1 = $this->Cars->pagination(1, 10)->order_by('name')->get_list();

Count

$brand1_cars_count = $this->Cars->where('brand_id', 1)->count();

Automatically Get a Foreign Key Object

$this->load->model('Brands');	// just another CI Powerful Model object
$car = $this->Cars->get(1);
$brand = $car->get_brand();	// cars.brand_id => brands.id
// uses the CI inflector's helper to transform <entity>_id to <entities>.id

Define Custom Methods

Here the code in Models/Cars_model.php:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Cars_model extends MY_Model {
	
	public function has_brand()
	{
		$this->db->where('brand_id >', 0);
		
		return $this;	// remember to return $this for method chaining support
	}
	
}

class Car_object extends Model_object {
	
	public function brand_name()
	{
		$CI = get_instance();
		
		$CI->load->model('Brands_model', 'Brands');	// just another CI Power Model object
		$brand = $CI->Brands->get($this->brand_id);
		if ($brand)
			return $brand->name;
		return '';
	}
}

Usege of the Defined Custom Methods

$cars = $this->Cars->has_brand()->get_list();
foreach ($cars as $car)
{
	echo 'Car: '.$car->name.', Brand: '.$car->brand_name().'<br>';
}

Support for CodeIgniter Query Builder Caching System

$this->Cars->start_cache();
$this->Cars->where('brand_id', 1);
$this->Cars->order_by('name');
$this->Cars->stop_cache();
$current_page_cars = $this->Cars->pagination(1, 10)->order_by('name')->get_list();
$total_cars_to_show = $this->Cars->count();	// this maintains the filter defined between start_cache() and stop_cache()
$this->Cars->flush_cache();

Delete

$to_delete = $this->Cars->get($id);
$to_delete->delete();

Created/Modified Datetime

If you add a created (datetime) and a modified (datetime) field in your table, CI Powerful Model automatically write the creation date and the last change date

Soft Delete Support

If you add a deleted (datetime) field in your table, the delete function doesn't hard delete the record, but writes the delete datetime in this field.
In this case, to filter your queries excluding the logical deleted records, call the all method before. Example:

$all_cars = $this->Cars->all()->get_list();
$some_cars = $this->Cars->all()->where_in('brand_id', array(1, 2, 3))->get_list();

codeigniter-powerful-model's People

Contributors

s2software avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.