CodeIgniter: Extending the native ‘Model’ and make it your own.

Today I took advantage of CodeIgniter’s ability to extend the native libraries, and I was well satisfied that it just works.

Let me elaborate, I’m in the process of creating models for my CI project, and realized that certain functions within the models were getting repetitive. Using CI’s ability to create my own custom libraries, I was able to create my own custom ‘Model’ which extends from the core ‘Model’ object. How this simple architecture has cleaned up my code is simply remarkable.

So read on…

CI has some pretty great documentation to get a beginner CI coder up to speed. In the case of using models, the docs says you need to extend from the CI’s core ‘Model’ object.

Typically the code will be something like this:

1
2
3
4
5
class Model_name extends Model {
	function Model_name() {
		parent::Model();
	}
}

Given that Models are generally library functions to your database, certain functions like create, read, update and delete would be common across all models. In which case the code will probably start to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Model_name extends Model {
 
	function Model_name() {
		parent::Model();
	}
 
	function create() {
		//do insert data into database
	}
 
	function read() {
		//do get data into database
	}
 
	function update() {
		//do update data into database
	}
 
	function delete() {
		//do delete data from database
	}
}

Imagine having to repeatedly write (opps, cut-and-paste) that same 4 (or more) functions to every model you create. A better way would be to consolidate those functions into a parent Model, and your models inherit from the parent their ability to create, read, update and delete.

One way you can do this is to just edit and insert these functions into CI’s native Model code which you can find in system/libraries/Model.php. But when it comes to upgrading the core when a new version is released, you may end up overriding those changes you need.

A much better way is to create your own model object called MY_Model and inherit it’s capabilities from the core Model. (Do note that ‘MY_’ is the default prefix set in CI for extending native libraries, but the prefix can be changed. Read the docs.)

This is how you do it, you create a new php file, MY_Model.php in the applications/libraries/ folder. The code for MY_Model would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MY_Model extends Model {
 
	function MY_Model() {
		parent::Model();
	}
	function create() {
		//do insert data into database
	}
 
	function read() {
		//do get data into database
	}
 
	function update() {
		//do update data into database
	}
 
	function delete() {
		//do delete data from database
	}
}

Now, within your models at applications/models folder, you would do something like this:

1
2
3
4
5
class Model_name extends MY_Model {
	function Model_name() {
		parent::MY_Model();
	}
}

And within your controllers at applications/controllers folder, you would be able to access the common functions of create, read, update and delete as you would normally.

1
2
3
4
5
class Blogs extends Controller {
	function view() {
		$this->Model_name->read();
	}
}

And that’s it. I know it’s very skeletal, but it should give you some ideas on how to proceed to extend the native core libraries CI comes with. Of course, this method doesn’t just apply to ‘Model’, you can extend any of the CI core objects. The CI docs have a good intro to extending the native library.

Also, I would like to give a nod to Emram at PHPFour for his Extended Model for CodeIgniter which basically incorporates CakePHP-like model capabilities into the CI Model.

Do bear in mind his method is to replace the system/libraries/Model.php file which is not recommended, for reasons I mentioned before. However, you should be able to incorporate his work by extending from the native library.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on November 7, 2008 at 5:46 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , ,

9 Responses

Subscribe to comments via RSS

  1. [...] CodeIgniter: Extending the native ‘Model’ and make it your own [...]

  2. Written by Jason on November 13, 2008 at 3:21 am
    Permalink

    Just a question….
    Why the use of Model_name() constructor instead of __construct()?

  3. Written by webmaster on November 13, 2008 at 9:03 am
    Permalink

    hi Jason

    I’m just sticking to the convention stated by CodeIgniter. CodeIgniter is written to be compatible with PHP 4. See the CI wiki
    http://codeigniter.com/user_guide/overview/at_a_glance.html

    Thanks

  4. Written by Jérôme on December 9, 2008 at 1:11 pm
    Permalink

    Thank you for that! It wasn’t working for me because I was trying to extend CI_Model instead of just Model :)

  5. Written by Md Emran Hasan on February 24, 2009 at 12:45 am
    Permalink

    I’m glad the author has pointed to the “Extended Model” solution by me. I’d like to know the readers that there is an updated which does not require you to replace the core library. Have a look:

    http://www.phpfour.com/blog/2009/02/updated-http-class-extended-model-codeigniter-cross-domain-ajax-php/

    Cheers!

  6. Written by Richard on March 25, 2009 at 8:03 am
    Permalink

    This is helpful…

    I think this kind of thinking is one of the qualities of CI, without being able to extend it as such would mean a far more limited framwork…

    After all we are only just playing with standard php classes and objects….

    Thanks for the post

  7. Written by welson santos on August 11, 2009 at 5:27 am
    Permalink

    hi friend, I tried to make this example and don’t run, I created one file with name MY_Model in folder application/liberies, and last I created other file that extends MY_Model e saved in folder application/models and don’t run

    where I wrong ?

  8. Written by anon on July 12, 2010 at 12:04 am
    Permalink

    Try this
    1. Move My_Model.php from application/libraries to application/model
    2. Add My_Model in application/config/autoload.php
    $autoload['model'] = array(‘My_Model’);

  9. Written by Usama Ahmed on August 27, 2010 at 12:57 pm
    Permalink

    @anon.

    Thanks. This worked.
    I have a question. What if we don’t want to name our model as ‘MY_Model’. What if i want my model to be as ‘MY_User’????

Subscribe to comments via RSS

Leave a Reply