Miscellaneous Code

  Home arrow Miscellaneous Code arrow Building a Generic Model for the CodeI...
MISCELLANEOUS CODE

Building a Generic Model for the CodeIgniter PHP Framework
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2009-08-12

    Table of Contents:
  • Building a Generic Model for the CodeIgniter PHP Framework
  • Defining the generic model's basic structure
  • Adding getters and setters to the generic model class
  • The new generic model class

  •  
     

    SEARCH CODEWALKERS

    Building a Generic Model for the CodeIgniter PHP Framework


    (Page 1 of 4 )

    Welcome to the first part of an eight-part series that, as you can tell from the title, shows you how to build a generic model class for CodeIgniter. In this part, I start by building an abstract model class that extends the native default model provided by CodeIgniter. This is a bare bones model, but I will provide it with more functionality in upcoming parts of the series.

    When it comes to speeding up the development of PHP applications, relying on a good, well-trusted framework can make a huge difference. Even though there are still many passionate programmers (including myself) who very often try to reinvent the wheel and create solutions for certain problems by means of a personal approach, the truth is that reusing classes and functions that have already been thoroughly tested and utilized in production environments by others is a real time saver.

    Of course, a PHP framework is quite possibly the ultimate expression of code reuse. It allows you to build complex PHP programs by reutilizing a set of classes, helpers, modules, etc, in accordance with certain predefined conventions -- which otherwise would be a nightmare to create from scratch. I've been using both CodeIgniter (http://codeigniter.com) and Kohana (http://kohanaphp.com) in the last few months for developing some small/medium-scaled PHP applications, and I must say that both of them have contributed to increasing my productivity while allowing me to maintain a decent level of control over my programming methodology. And that's a good thing, believe me.

    CodeIgniter still lacks a feature that has been requested by numerous users on its forums: the implementation of a native ORM class (ORM stands for Object Relational Model). While it's perfectly feasible to create custom models that will interact with each table of a database-driven application developed with CI, it's fair to say that this can be a pretty redundant task for larger projects. For instance, imagine that you need to build a program that will handle data stored on ten different MySQL tables. In a case like this, you'll need to define ten models for the tables in question that probably will share some common logic for performing CRUD operations. Pretty annoying, right?

    However, as you might have already guessed, this issue can be solved simply by creating a generic model class that encapsulates much of the common logic required to interact with a single database table, and then inheriting from it as many child models as necessary. In doing so, in some cases the signature of the child models could be as simple as overriding only the parent's constructor.

    Having outlined the advantages in having a generic model for CodeIgniter, in this group of articles I'm going to build this piece of software in a step-by-step fashion. I'll start out defining a class that will implement a few straightforward methods, suited for fetching, inserting, updating and deleting rows of a database table, and then I'll implement some additional features, like validation of the model's input data and basic error handling capabilities as well.

    Finally, I'd like to thank to the CodeIgniter development team, as well as Simon Stensi and Emran Hasan, which have been a part of my source of inspiration for writing this model class. That being said, it's time to get rid of the preliminaries and begin learning how to create this generic model for CodeIgniter. Let's get going!

    More Miscellaneous Code Articles
    More By Alejandros Gervasio

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap