Model library for CodeIgniter

A set of methods to speed up your development for CodeIgniter 2 (Go to 1.x version).

Model library

application/core/MY_Model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
// http://jeromejaglale.com/doc/php/codeigniter_models
 
class MY_Model extends CI_Model {
 
        var $table = "";
 
        function __construct()
        {
                parent::__construct();
                $this->load->database();
        }
 
        function insert($data)
        {
                $this->db->insert($this->table, $data);
                return $this->db->insert_id();
        }
 
        function find_id($id)
        {
                if ($id == NULL)
                {
                        return NULL;
                }
 
                $this->db->where('id', $id);
                $query = $this->db->get($this->table);
 
                $result = $query->result_array();
                return (count($result) > 0 ? $result[0] : NULL);
        }
 
        function find_all($sort = 'id', $order = 'asc')
        {
                $this->db->order_by($sort, $order);
                $query = $this->db->get($this->table);
                return $query->result_array();
        }
 
        function update($id, $data)
        {
                $this->db->where('id', $id);
                $this->db->update($this->table, $data);
        }
 
        function delete($id)
        {
                if ($id != NULL)
                {
                        $this->db->where('id', $id);                    
                        $this->db->delete($this->table);                        
                }
        }       
}
 
/* End of file */

Use

Create your model

application/models/book.php

<?php
class Book extends MY_Model {
 
	function __construct()
	{
		parent::__construct();
		$this->table = 'book';
	}
}
/* End of file */

In your controller

insert

$this->load->model('book');
 
$book = array();
$book['title'] = 'The Black Dahlia';
$book['author'] = 'James Ellroy';
 
$this->book->insert($book);

find by id

$this->load->model('book');
 
$book = $this->book->find_id('47');
 
echo $book['title'];

find list

$this->load->model('book');
 
$book_list = $this->book->find_all('title', 'asc');
 
foreach ($book_list as $book)
{
  echo $book['title'];
}

delete

$this->load->model('book');
 
$this->book->delete('47');
 

Feedback

Nice! Looking forward to trying this out.
Bob
Mar 19, 2009
#1
That's much cleaner and logical
loopion
Mar 20, 2009
#2
Great, thanks for sharing. We were only talking about this the other day
Dave
Jul 26, 2009
#3
Wonderful work,it really help me out
Said
Sep 30, 2009
#4
Very nice! I don't know why CI hasn't included this by default...
Bijan
Nov 3, 2009
#5
Great job!
Claudio
Dec 10, 2009
#6
Hi, i'm from Argentina (sorry about my english). What about add a class variable $id. Not everyone uses "id" in all tables... Bye!
Matias
Dec 21, 2009
#7
Nice work! #7 What else would you use?
Pete
Dec 24, 2009
#8
Matias (#7) : yes, that would be useful, especially with an existing database. Thank you for sharing this idea. However, it's easy to do if you need it, so I'm not updating the code above.
Jérôme Jaglale
Dec 26, 2009
#9
Hi, found this code really useful but my current project uses two databases so I updated it to handle multiple dbs.
database = $this->load->database($db,TRUE);
    }
 
 function insert($data)
 {
  $this->database->insert($this->table, $data);
  return $this->database->insert_id();
 }
 
 function find_id($id)
 {
  if ($id == NULL)
  {
   return NULL;
  }
 
  $this->database->where('id', $id);
  $query = $this->database->get($this->table);
 
  $result = $query->result_array();
  return (count($result) > 0 ? $result[0] : NULL);
 }
 
 function find_all($sort = 'id', $order = 'asc')
 {
  $this->database->order_by($sort, $order);
  $query = $this->database->get($this->table);
  return $query->result_array();
 }
 
 function update($id, $data)
 {
  $this->database->where('id', $id);
  $this->database->update($this->table, $data);
 }
 
 function delete($id)
 {
  if ($id != NULL)
  {
   $this->database->where('id', $id);   
   $this->database->delete($this->table);   
  }
 } 
}
 
/* End of file MY_Model.php */
/* Location: ./system/application/libraries/MY_Model.php */
Just select the db in the model like this:
parent::MY_Model('default');
Jim
Jan 20, 2010
#10
I prefer the following update function:

function update($newdata, $where){
$this->db->update($this->table, $this->data, $where);
return $this->db->affected_rows();
}

So I can update many rows with custom where clause
Permana Jayanta
Apr 4, 2010
#11

function update($newdata, $where){
$this->db->update($this->table, $this->data, $where);
return $this->db->affected_rows();
}

So I can update many rows with custom where cl

Jun 8, 2011
#12
It's cool to see, that I actually can use all the database functionality in a library, wasn't sure of that. Cool lib also, even though I don't have use for it right now. Nice job!
Jonathan
Aug 4, 2011
#13
Nice work! I got an idea from this post.
Thz
MyoKo
Aug 12, 2011
#14
works perfectly on localhost..
but when I upload to server
I get My_Model not found ... :(

Help Me!!!!! Mayday Mayday....
Ali Turab Gilani
Sep 3, 2011
#15
#15
check file name of your my_model class,
linux server is case sensitive file name.
mas Nug
Oct 17, 2011
#16
Where is the function Update in the controller?
Luis
Oct 19, 2011
#17
Im from El Salvador, can you help me, please? Jérôme Jaglale
Luis
Oct 19, 2011
#18
Lots of Thanks ...................
Above example is very simple , basic and help full
Omi
Oct 28, 2011
#19
Esto ya no funciona mas en la version de codeigniter 2.X ya que la clase Model ha sido movida a la carpeta system/core/, ahora hay que mover la libreria a application/core/

un saludo y suerte... por cierto gracias por la libreria.

dhamasito
Dec 16, 2011
#20
it is really very helpfull during programming.
gaganpreetkaur
Jun 27, 2012
#21
Very nicely written article and very good example of all the good things about OOP! Thanks and well done!

Jul 30, 2012
#22
Definitely going to use this next time. Cheers!
David
Sep 19, 2012
#23
thanx
name
Jul 6, 2013
#24
Simple and effective, great library :)
Kinta mahadji
Feb 6, 2014
#25