Home arrow PHP arrow Finishing a Data Mapper Class for the Data Mapper Design Pattern

Finishing a Data Mapper Class for the Data Mapper Design Pattern

In this penultimate part of the series on the data mapper design pattern, I demonstrate how to provide the UserMapper class we've previously created with the ability to delete users from its associated MySQL table.

  1. Finishing a Data Mapper Class for the Data Mapper Design Pattern
  2. Review: mapping domain objects to database tables
  3. Enabling the user mapper class to delete users
  4. The user mapper class's full source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 31, 2010

print this article



Superbly described in Martin Fowler's book "Patterns of Enterprise Application Architecture," Data Mapper is a powerful database access pattern aimed at keeping domain objects isolated from the underlying storage mechanism (in most cases a relational database). This permits developers to build multi-layered applications that are much easier to maintain and scale.

Despite the pattern's intimidating name, building basic data mapper classes in PHP 5 is a fairly straightforward process for any programmer with an average background in the object-oriented paradigm. On the other hand, it's fair to say that fully-featured Object Relational Mappers (AKA ORMs) are hard-to-tame creatures that demand a lot of programming hours.

So, if you're planning to build one of these on your own, think twice before swimming in troubled waters and look at some of the well-trusted third party ORMs available, like Doctrine and Propel, which will take care of the hard work for you.

Naturally, my intention here isn't to demonstrate how to build an ORM from scratch. Instead, my plan is focused on explaining how to create simple data mapper classes, so you can see how useful they can be when it comes to working with domain objects that know nothing about the storage mechanism used to persist between requests.

In keeping with that goal, in the previous tutorial I defined a concrete data mapper class that partially performed CRUD operations against a MySQL table containing data on some fictional users. In simple terms, this mapper behaved as a bridge between user domain objects and the MySQL table.

However, the user mapper in its current state is still incomplete, as it does not have the ability to delete a specified user from its associated table. To address this issue, in the following lines I'm going to append to the mapper a method that will perform user deletions in a simple manner.

Ready to learn how this will be done? Then, let's get started right now!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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