Mohammad Emran Hasan
Mohammad Emran Hasan

Mohammad Emran Hasan

Extended Model for CodeIgniter

Mohammad Emran Hasan's photo
Mohammad Emran Hasan
·Jul 11, 2008·

3 min read

Feb 2009: An updated version of the code for CodeIgniter 1.7.x can be found here.

I developed this extension of CodeIgniter’s Model last year, but never had the chance to publish it. The main purpose of this extension is to make a dev’s life easy. This extension has been used by several of my devs at RBS and has been proved to increase productivity and reduce the number of painful small queries to write. Their enthusiasm has driven me to post this for the CI fans out there.

Without much babble, let’s get into point. I’ve explained the process of installing it and then showed some example uses. For starter, click here to download it. Now follow these steps to get started:

1. Replace the system/libraries/Model.php file with the attached Model.php (CodeIgniter version 1.6.3)

2. For each of your tables, you will need to create a model file in system/application/models.

3. Lets say we have a “products” table whose schema is as follows:

CREATE TABLE `products` ( `id` int(11) NOT NULL auto_increment, `title` varchar(50) NOT NULL, `description` text NOT NULL, `color` varchar(20) NOT NULL, `price` float NOT NULL, `category_id` int(11) NOT NULL, `featured` char(1) NOT NULL, `enabled` char(1) NOT NULL, `visits` int(1) NOT NULL, `created` int(11) NOT NULL, `modified` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM;

4. Now we need to create system/application/models/product.php:

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

class Product extends Model { function Product() { // Call the Model constructor parent::Model();

    // Load the associated table
    $this->loadTable('products');
}

}

5. From any controller, you can load the Model as instructed in the CI manual. Here are some sample usage of the model functions:

function products() { // Load the model in the default way $this->load->model('Product');

// Total # of products
echo $this->Product->findCount();

// Total # of featured products
echo $this->Product->findCount("featured = 'Y'"); 

// Retrieve ALL the products
$allProducts = $this->Product->findAll(); 

// Retrive id, title and price of top 10 products (based on popularity) that are enabled
$topProducts = $this->Product->findAll("enabled = 'Y'", 'id, title, price', 'visits DESC', '0', '10');

// Retrive id, title and price of the 1st most popular product that is enabled
$topProducts = $this->Product->find("enabled = 'Y'", 'id, title, price', 'visits DESC', '0', '10'); 

// Retrieve the product with id = 1
$oneProduct = $this->Product->read(1); 

// Retrive the price of the product whose id = 1
$productPrice = $this->Product->field('price', 'id = 1'); 

// Single array with the titles of all the enabled products
$productArr = $this->Product->generateSingleArray("enabled = 'Y'", 'title'); 

// Insert a new product in the db
$newProductId = $this->Product->insert(array('title' => 'New Product', 'price' => '10.99')); 

// Update the price of the newly added product
$updProduct = $this->Product->save(array('price' => '20.00'), $newProductId); 

// Delete the product
$this->Product->remove($new\_product\_id);

}

7. There are a number of other helpful functions in this file. If you have a careful look, you’ll discover that some of them are really handy.

UPDATE: I forgot to give due credit to the wonderful developers of CakePHP – I’ve taken inspiration from their Model implementation while building this one for CodeIgniter.

Download

MORE UPDATE: Download the version for CodeIgniter 1.7.x here.

 
Share this