Home arrow PHP arrow Page 3 - Building the Data Mapper Classes for the Data Mapper Design Pattern

Building an abstract data mapper class - PHP

In this third part of a five-part series on the data mapper design pattern, I start developing a basic user mapper class. This class allows you to map user domain objects to their associated database table in a fairly easy way.

  1. Building the Data Mapper Classes for the Data Mapper Design Pattern
  2. Review: a quick look at some previous sample classes
  3. Building an abstract data mapper class
  4. Mapping concrete user domain objects with a user mapper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 24, 2010

print this article



In the section that you just read, I promised that you’d learn how to build a data mapper class from scratch. Well, in keeping with that, please look at the following code fragment. It defines the generic structure of an abstract mapper:  







abstract class DataMapperAbstract


    protected $_db = NULL;

    protected $_table = '';

    protected $_identityMap = array();


    public function __construct(MySQLAdapter $db)


        $this->_db = $db;   



    // get domain object by ID (implemented by concrete domain object subclasses)

    abstract public function find($id);


    // insert/update domain object (implemented by concrete domain object subclasses)

    abstract public function save(DomainObjectAbstract $domainObject);


    // delete domain object (implemented by concrete domain object subclasses)

    abstract public function delete(DomainObjectAbstract $domainObject);


Definitely, the definition of the above “DataMapperAbstract” class brings some interesting things to the table that deserve a deeper analysis. As you can see, this class defines a clean, abstract interface, which via concrete implementations, will perform CRUD operations on domain objects by using an instance of the MySQL adapter.

The class not only uses a $_table property to map those operations to a specified database table, but defines another property called $_identityMap. As you’ll see in a moment, this latter will be used for building an identity map to keep track of what objects were fetched from the database.

If you're interested in a fuller explanation of  the Identity Map and other design patterns, I suggest to you grab a copy of Martin Fowler’s book, Patterns of Enterprise Application Architecture, or visit Fowler's Identity Map page.

So far, so good. Having defined an abstract mapper class, the next logical step is to create a concrete one that permits you to work directly with user domain objects. That will be the topic that I’m going to discuss in the next section.

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