Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 3 - A Web App Based on a Model for the Cod...
MISCELLANEOUS CODE

A Web App Based on a Model for the CodeIgniter PHP Framework
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-09-30

    Table of Contents:
  • A Web App Based on a Model for the CodeIgniter PHP Framework
  • Review: the generic model class’s complete source code
  • Inheriting the functionality of the abstract model class
  • The property model class in action

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    A Web App Based on a Model for the CodeIgniter PHP Framework - Inheriting the functionality of the abstract model class


    (Page 3 of 4 )

    Undeniably, all of the hard work that went into building a generic model class is worth it when the model is used for building a database-driven program. In this  case, the example real estate application that I’m planning to build will utilize only one MySQL database table that will store some fictional properties, but imagine the time you’ll save if your application is going to work with multiple tables.

    However, the best way to understand the benefits of having an abstract model class is by creating a sub class from it, which as I said before will retrieve, save and delete a few properties. The signature of this child model, which I called “Property_model,” has been listed below. Have a look at it:

    <?php

    class Property_model extends MY_Model

    {

    function __construct()

    {

    parent::__construct();

    // Sets data of table associated to the model

    $this->setTableData('properties');

    // Sets validation rules for the model

    $this->setValidation(array('title' => 'required', 'short_description' => 'required'));

    }

    }

    // END Property_model Class

     

    /* End of file Property_model.php */

    /* Location: ./system/application/models/property_model.php */

    As you can see, it's very short and easy to define a model class that interacts with a specified database table when you have a generic model at your disposal. In this case, the above “Property_model” class only sets within the constructor the name of the MySQL table with which it’s going to work, along with the validation rules that will be applied to the data used for inserting and updating properties. That's all.

    Of course, the advantages in relying on an abstract model class will be seen more clearly when working with multiple database tables. In that situation, you’ll only need to define short and simple child models for each table. Anyway, the previous example should give you an appropriate idea of how useful a generic model can be for quickly building complex database-driven applications with CodeIgniter.

    So far, everything looks good, right? At this point, you surely grasped the logic that drives the “Property_model” child model, meaning that it’s time to see how it can be used within a controller class to retrieve, save and delete properties in a truly simple fashion.

    So, if you’re interested in learning how this controller will be built, read the section to come. It’s only one click away.

    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 ...


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