Home arrow PHP arrow Using the Active Record Pattern with PHP and MySQL

Using the Active Record Pattern with PHP and MySQL

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.

TABLE OF CONTENTS:
  1. Using the Active Record Pattern with PHP and MySQL
  2. Building a simple data mapper in PHP
  3. Building a basic MySQL abstraction class
  4. Inserting database table rows with the active record pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
March 03, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: