Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 3 - Fetching Database Rows with a Model fo...
MISCELLANEOUS CODE

Fetching Database Rows with a Model for the CodeIgniter PHP Framework
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Fetching Database Rows with a Model for the CodeIgniter PHP Framework - Implementing the fetch() method


    (Page 3 of 4 )

    Since the method of the model class that I’m about to code will be responsible for fetching rows from the associated database table, I decided to call it simply “fetch(),” but it’s usual to name it “get(),” “find(),” and so forth. Basically, this method will perform SELECT statements in accordance with certain query modifiers, which will be passed in to it as a succession of arguments.

    That being explained, here’s the implementation of the aforementioned “fetch()” method. Take a look at it:

    /**

    * Returns a result set with specified fields according to given conditions.

    *

    * @author Alejandro Gervasio

    * @return query result on success - Boolean FALSE on failure

    * @access public

    */

    public function fetch($fields = '*', $where = NULL, $order = 'id ASC', $limit = NULL, $offset = 0, $join = NULL)

    {

    if ($fields != '*')

    {

    $this->db->select($fields);

    }

    if ($this->id != NULL)

    {

    $this->db->where('id', $this->id);

    }

    elseif($where != NULL)

    {

    $this->db->where($where);

    }

    if ($order != 'id ASC')

    {

    $this->db->orderby($order);

    }

    if ($limit != NULL)

    {

    $this->db->limit($limit, $offset);

    }

    if( $join != NULL)

    {

    $this->db->join($join);

    }

    $query = $this->db->get($this->table);

    $this->numRows = $query->num_rows();

    if ($this->numRows > 0)

    {

    return ($this->numRows > 1 ) ? $query->result() : $query->row();

    }

    $this->errors[] ='No rows were returned by the query.';

    return FALSE;

    }

    As I explained previously, this method will return a result set from the specified database table in accordance with different arguments, which has been assigned a default value that varies for each particular case. Also, it’s worthwhile to note that the workhorse of this method is CodeIgniter’s Active Record class, so again I recommend that you read the pertinent user guide if you’re not familiar with it.

    Finally, it’s valid to mention that if the “$this->id” property of the model has been set in the proper controller, then it’ll return the row that matches that ID. Otherwise, it’ll return a multi-row data set if the group of conditions passed in as parameters produces a valid result.

    Undeniably, the implementation of the “fetch()” method is very intuitive, meaning that you shouldn’t have major trouble grasping its logic. However, as with everything in life, it can be improved or even completely overridden, but that will be left as homework for you.

    At this point, I've already shown you the signature of one of the core methods of the model class. Therefore, it’s time to go one step forward and list the complete source code of the class in question, this time including the “fetch()” method that you just saw a few moments ago.

    This will be accomplished in the final section of this article, so click on the link below and read the following segment.

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