Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - 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

    TOOLS YOU CAN USE

    advertisement

    Building a Generic Model for the CodeIgniter PHP Framework - The new generic model class


    (Page 4 of 4 )

    As I stated in the previous section, it'd be really helpful to list the complete signature of the generic model class in its current state, so you can see at a glance how it looks so far. With that idea in mind, below I included the class in question. Here it is:

    abstract class MY_Model extends Model

    {

    protected $table = ''; // table associated to the model

    protected $fields = array(); // fields of table associated to the model

    protected $id = NULL; // value of the primary key of the table associated to the model

    protected $data = array(); // model input data

    protected $insertID = NULL; // insertion ID

    protected $numRows = NULL; // number of rows returned by SELECTS

    protected $validation = array(); // model validation rules

    protected $errors = array(); // model errors

     

    /**

    * Constructor

    *

    * @access protected

    */

     

    protected function __construct()

    {

    parent::Model();

    // get CI super object as a model property

    $this->ci =& get_instance();

    }

     

    /**

    * Sets associated table data for the model

    *

    * @author Alejandro Gervasio

    * @return void

    * @access public

    */

     

    public function setTableData($table = 'default')

    {

    if ($this->db->table_exists($table))

    {

    $this->table = $table;

    $this->fields = $this->db->field_names($this->table);

    }

    }

     

    /**

    * Sets value of primary key of the associated table for the model

    *

    * @author Alejandro Gervasio

    * @param integer

    * @return void

    * @access public

    */

     

    public function setID($id)

    {

    $this->id = is_integer($id) AND $id > 0 ? $id : 1;

    }

     

    /**

    * Gets value of primary key of the associated table for the model

    *

    * @author Alejandro Gervasio

    * @return integer

    * @access public

    */

     

    public function getID()

    {

    return $this->id;

    }

    }

    Admittedly, the generic model class in its current state isn't very functional, but as I expressed in the introduction, this a progressive development process, meaning that there's still a long way ahead of us. Of course, the class needs to implement several methods that will be used for performing at least some CRUD operations against its associated database table. However, these will be incorporated in forthcoming articles of the series. So, be patient for the moment, and meanwhile, don't forget to save the model under the /system/application/libraries/ folder of your own CodeIgniter installation.

    Final thoughts

    In this introductory chapter of the series, I started building an abstract model class that extends the native default model provided by CodeIgniter. Even when at this time, my class in pretty useless, I'm going to provide it with more functionality in upcoming parts of the series.

    Precisely, in the next article I'm going to define and implement a few other methods. We'll use these methods for setting the validation rules for the generic model, as well as for storing the data in the form of an array that will be utilized for inserting and updating rows of its associated table.

    Now that you know what the following article will be about, you can't miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    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 9 - Follow our Sitemap