Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 3 - 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 - Adding getters and setters to the generic model class


    (Page 3 of 4 )

    In the previous segment, I briefly described the meaning of each property declared within the custom model as well as the basic implementation of its constructor. Now, it's time to extend the model's initial signature by adding to it some additional methods that will be used for defining the name of the associated database table and its corresponding fields, as well as for setting and retrieving the value of the ID field. So, in summary, here's how these brand new methods look. Check them out:

    /**

    * 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;

    }

    As you can see above, the first setter method, called "setTableData()," will assign the name of the database table associated with the generic model to the pertinent $table property, along with its corresponding fields. In addition, it's fair to notice that this method (and others that will be defined in upcoming tutorials) makes use of CodeIgniter's database class, in this case referenced as $this->db. Thus, if you're not familiar with this class, I suggest that you take a look at the CI user guide to learn more about it.

    Now, returning to the remaining "setID()" and "getID()" methods, they behave as simple wrappers for setting and retrieving the value of the id of the table row to which the custom model points. Since these are undoubtedly very easy to grasp, I'm not going to waste more of your time explaining how they work.

    Well, at this point the custom model looks a bit more functional, even though it's still in an immature state. However, it would be useful to list its complete signature, this time including the methods that you saw before. This process will be accomplished in the section to come, so click on the link below and read the next few lines.

    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 5 - Follow our Sitemap