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://maestric.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 MY_Model.php */
/* Location: ./system/application/libraries/MY_Model.php */

Use

Create your model

system/application/models/book.php

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

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
March 19, 2009
#1
That's much cleaner and logical
loopion
March 20, 2009
#2
Great, thanks for sharing. We were only talking about this the other day
Dave
July 26, 2009
#3
Wonderful work,it really help me out
Said
September 30, 2009
#4
Very nice! I don't know why CI hasn't included this by default...
Bijan
November 3, 2009
#5
Great job!
Claudio
December 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
December 21, 2009
#7
Nice work! #7 What else would you use?
Pete
December 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
December 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
January 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
April 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

June 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
August 4, 2011
#13
Nice work! I got an idea from this post.
Thz
MyoKo
August 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
September 3, 2011
#15
#15
check file name of your my_model class,
linux server is case sensitive file name.
mas Nug
October 17, 2011
#16
Where is the function Update in the controller?
Luis
October 19, 2011
#17
Im from El Salvador, can you help me, please? Jérôme Jaglale
Luis
October 19, 2011
#18
Lots of Thanks ...................
Above example is very simple , basic and help full
Omi
October 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
December 16, 2011
#20