Monday, October 3, 2011

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

Hi All,
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:
-----
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:
------
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:
--------
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:
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.
------
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.
Contact:
bhavinrana07[@]gmail.com


1 comment:

  1. Nice help Wonder full Post bhavin !
    this post helped me Many thanks to Blogger 1

    ReplyDelete

Any Questions or Suggestions ?

About

Professional & Experienced Freelance Developer From India, Technologist, Software Engineer, internet marketer and Open Sources Developer with experience in Finance, Telecoms and the Media. Contact Me for freelancing projects.

Enter your email address:

Delivered by FeedBurner