Model library for CodeIgniter

A set of methods to speed up your development.

Model library

system/application/libraries/MY_Model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Model extends Model {
 
	var $table = "";
 
    function My_Model()
    {
        parent::Model();
	$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 Book()
	{
		parent::MY_Model();
		$this->table = 'book';
	}
 
/* End of file book.php */
/* Location: ./system/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