Home arrow PHP arrow Refactoring the MySQL Abstraction Class with the Active Record Pattern

Refactoring the MySQL Abstraction Class with the Active Record Pattern

In this group of articles youíll be provided with an approachable guide to building a database accessing class that will use the programming model imposed by the active record pattern for performing raw CRUD operations on a group of selected MySQL tables, and for fetching database records by using the WHERE, LIKE and LIMIT clauses as well. This is the sixth part of a seven-part series.

TABLE OF CONTENTS:
  1. Refactoring the MySQL Abstraction Class with the Active Record Pattern
  2. Refactoring the MySQL sample class: modifying methods that perform CRUD operations
  3. Completing the refactoring process of the MySQL abstraction class
  4. Manipulating database records using the improved version of the MySQL class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 07, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Introduction

When it comes to implementing the active record design pattern with PHP, in most cases data mapper objects are used to interact with a certain number of databases. Generally, these objects act like a single, unified interface that can be used to perform CRUD tasks (Create, Retrieve, Update, Delete) on mapped tables.

This schema obviously permits you to achieve a high level of SQL abstraction, since executing the mentioned tasks against a selected database table doesnít require directly writing any SQL statements. All the business logic is handled behind the scenes by the objects in question.

However, itís perfectly feasible to apply the active record pattern by using just a database accessing class, and still get an acceptable grade of SQL abstraction, particularly when it comes to inserting, updating and deleting records of a specific database table.

Now, returning for a moment to the topics discussed in the last installment, I left off explaining how to build a simple MySQL abstraction class, which had the ability to fetch a predefined number of records from a sample ďusersĒ MySQL table by using a LIMIT clause. In this particular case, a concrete method within the class was responsible for performing this task, without the need to code explicitly any SQL statements from outside the class.

As I stated in the end of the tutorial, however, some methods of this sample class implement redundant business logic, and as a consequence itís necessary to refactor them to fix this issue in a quick and simple manner. Thus, in the next few lines Iíll be explaining how to accomplish this process progressively, and as always, accompanied by the corresponding code samples.

Now, itís time to start modifying the signature of this MySQL abstraction class to make it more efficient and compact. Letís go!



 
 
>>> 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: