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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: