PHP
  Home arrow PHP arrow Page 3 - Defining a Model Class for Handling Views with CodeIgniter
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Defining a Model Class for Handling Views with CodeIgniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-04-23


    Table of Contents:
  • Defining a Model Class for Handling Views with CodeIgniter
  • Review: moving presentation logic out of views
  • Handling database contents with a simple model class
  • Demonstrating a simple use of the model class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Defining a Model Class for Handling Views with CodeIgniter - Handling database contents with a simple model class
    ( Page 3 of 4 )

    In the preceding segment, I mentioned that it was perfectly possible to incorporate a model class to access database contents, instead of directly using the database class included with CodeIgniter. So, to put this theoretical concept into practice, below I created such a class. In this case, it is charged with fetching some rows from the “users” MySQL table that you learned before, and with counting its records as well.

    In summary, this model class, called “User_model,” is defined in the following way:


    <?php

    class User_model extends Model{

    function User_model(){

    // call the Model constructor

    parent::Model();

    // load database class and connect to MySQL

    $this->load->database();

    }

    // fetch all users

    function getAll(){

    $query=$this->db->get('users');

    // return result set as object

    return $query->result();

    }

    // fetch some users based on a predefined condition

    function getWhere($field,$param){

    $this->db->where($field,$param);

    $query=$this->db->get('users');

    // return result set as object

    return $query->result();

    }

    // get total number of users

    function getNumber(){

    return $this->db->count_all('users');

    }

    }

    ?>


    In reality, understanding how the above “User_model” class works is a pretty straightforward process, don’t you think? As you can see, this model implements a few basic methods for retrieving users from the corresponding database table, and for counting them as well. Naturally, it’s feasible to aggregate more methods that perform inserts, updates and deletions, but for now I’ll keep the signature of the model simple.

    So far, so good. At this point, I showed you how to build a basic model class that will be responsible for fetching user-related data from the pertinent MySQL table. Therefore, the next step is to change the signature of the “WebPage” controller class so that it can use the model for accessing database contents.

    This modification will be discussed in detail in the last section of this tutorial. Therefore, please click on the link below and read the upcoming segment.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





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