HomePHP 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.
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!