Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 3 - Completing a Model for the CodeIgniter...
MISCELLANEOUS CODE

Completing 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-23

    Table of Contents:
  • Completing a Model for the CodeIgniter PHP Framework
  • Performing generic SQL queries
  • Completing the AbstractModel class
  • The generic model class's full source code

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Completing a Model for the CodeIgniter PHP Framework - Completing the AbstractModel class


    (Page 3 of 4 )

    As I stated in the section that you just read, the model needs to be able to resent its properties, and build the error strings that will be shown when validating incoming data and performing CRUD operations as well. With that idea in mind, below I included the definition of a few extra methods that accomplish all of these things in a pretty straightforward fashion. Look at them, please:

    /**

    * Gets model errors

    *

    * @author Alejandro Gervasio

    * @param string

    * @return string

    * @access public

    */

    public function getErrors($errPrefix = '<p>')

    {

    // if no errors where found return an empty string

    if( count($this->errors) === 0)

    {

    return '';

    }

    // otherwise build and return error string

    $errorString = '';

    $errorSufix = str_replace( '<', '</' , $errPrefix);

    foreach ($this->errors as $error)

    {

    $errorString .= $errPrefix . $error . $errorSufix;

     

    }

    return $errorString;

    }

     

    /**

    * Clears model properties

    *

    * @author Alejandro Gervasio

    * @return void

    * @access public

    */

    public function clear()

    {

    $this->id = NULL;

    $this->data = array();

    $this->errors = array();

    $this->validation = array();

    }

    As illustrated above, I coded a couple of extra methods, called “getErrors()” and “clear()” respectively. The first one is charged with building the error messages that will be displayed to users when certain types of failures occur, and the last one is responsible for resetting the model’s state. Of course, both of these methods can be enhanced in all sorts of clever ways, but for now this will be left as homework for you.

    Well, after incorporating the previous methods into the generic model class, it's nearing completion and to be fair, it’s almost ready to be used in a real-world environment. However, the complete source code of the class will be listed in the last section of this tutorial, thus finishing its development.

    So, if you wish to see how the completed class will look, go to the following segment and keep reading.

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