Miscellaneous Code
  Home arrow Miscellaneous Code arrow Page 2 - Fetching Database Rows with a Model fo...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

Fetching Database Rows with 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-08-26

    Table of Contents:
  • Fetching Database Rows with a Model for the CodeIgniter PHP Framework
  • Review: the current AbstractModel class
  • Implementing the fetch() method
  • The complete source code of the generic model class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Fetching Database Rows with a Model for the CodeIgniter PHP Framework - Review: the current AbstractModel class


    (Page 2 of 4 )

    In case you still haven’t had the chance to read the preceding article, and the signature of the generic model class developed then doesn’t ring any bells for you, below I included its complete source code, so you can quickly grasp how it functions. Take a moment to analyze the following class closely:

    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;

    }

     

    /** Sets input data for the model

    *

    * @author Alejandro Gervasio

    * @param array

    * @return void

    * @access public

    */

     

    public function setData($data)

    {

    if ( is_array($data) AND count($data) > 0)

    {

    foreach ($data as $key => $value)

    {

    if (array_search($key, $this->fields) === FALSE)

    {

    unset($data[$key]);

    }

    }

    $this->data = $data;

    }

    }

     

    /**

    * Sets validation rules for model data

    *

    * @author Alejandro Gervasio

    * @param array

    * @return void

    * @access public

    */

     

    public function setValidation($validation)

    {

    if ( is_array($validation) AND count($validation) > 0)

    {

    foreach ($validation as $field => $rule)

    {

    if (array_search($field, $this->fields) === FALSE)

    {

    unset($validation[$key]);

    }

    }

    $this->validation = $validation;

    }

    }

    }

    True to form, understanding how each of the methods that comprise the generic model class works should be a straightforward process for you, since they’re quite easy to follow. In the case of the constructor, it simply grabs an instance of the CodeIgniter super object and stores it as a class property, which will be utilized later on when working with some of its native classes.

    The other methods are mostly simple setters and getters that permit you to assign the proper values to the data that will be used by the model for inserting and updating database rows, as well as for setting the validation rules that will be applied by the model. Not too hard to understand, right?

    Well, now that you surely have a clearer idea of how the previous “AbtractModel” class does its business, it’s time to continue adding more functionality to it. Thus, as I expressed in the introduction, in the section to come I’m going to code a whole new core method that will be tasked with fetching rows from the specified database table in accordance with certain conditions.

    To learn how this method will be implemented, click on the link below and keep reading.

    More Miscellaneous Code Articles
    More By Alejandros Gervasio


       · This part of the series shows how to incorporate a brand new method into the model,...
     

    MISCELLANEOUS CODE ARTICLES

    - 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 ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek