PHP
  Home arrow PHP arrow Using the Active Record Pattern with PHP and MySQL
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

Using the Active Record Pattern with PHP and MySQL
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2009-03-03


    Table of Contents:
  • Using the Active Record Pattern with PHP and MySQL
  • Building a simple data mapper in PHP
  • Building a basic MySQL abstraction class
  • Inserting database table rows with the active record pattern

  • 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


    Using the Active Record Pattern with PHP and MySQL
    ( Page 1 of 4 )

    Using the active record pattern to develop a database-driven application offers many advantages. It can save PHP developers a wealth of time in coding and code maintenance, because you can take advantage of SQL abstraction. This first part of a series will walk you through the basics of using the active record pattern.

    Introduction

    According to Wikipedia, “the active record pattern is a design pattern frequently found in software that stores its data in relational databases. Active record is an approach to accessing data in a database.” And admittedly, this definition accurately outlines the functionality of this pattern, even though it sounds hard to grasp at first. 

    In many cases (though not all), implementing the active record pattern for a given web application requires the creation of a class instance that will be tied to each row of a database table. Based upon this schema, this instance will be used basically to select, update, insert and delete database records (also known as CRUD operations), without having to explicitly write any SQL statements.

    Obviously, one of the major advantages in using the active record pattern when developing a database-driven application is the one described above: SQL abstraction. Instead of having to code raw, hard-to-maintain selects, inserts, updates, and deletion operations at different points of the application to access data stored in a database, the methods of a data mapper object are used to encapsulate all the SQL code necessary to perform all these tasks “behind the scenes.”

    Utilizing data mapping objects isn’t the only way to apply the active record pattern, since it’s also feasible to achieve similar results by means of a database abstraction class that implements a bunch of methods that permit interaction with database tables without having to code SQL clauses from scratch.

    Even though this approach isn’t as popular as using data mappers, it has been adopted progressively by many programmers nowadays. In the case of PHP, popular frameworks like CakePHP and Code Igniter use this approach for implementing active record classes in a pretty successful way.

    Thus, in this series of articles, I’ll be explaining how to develop an active record class in PHP, which will allow you to perform selects, updates, inserts, and deletion operations against a selected MySQL database table in a simple fashion. As is typical of my approach, I'll use numerous code samples to make my points clear. 

    I’ll start with a class written in PHP 4 that will implement a few simple methods, and then, when you understand how it works, I’ll add more complexity and functionality.

    Are you ready to learn how to apply the active record pattern with PHP? Then don’t waste more time in preliminaries; start reading now!



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

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





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