Home arrow PHP arrow Page 4 - The Data Mapper Design Pattern: A Final Example

Putting the user mapper class to work - PHP

Welcome to the final part of a series on implementing the Data Mapper design pattern in PHP 5. This series teaches you the key concepts that surround the development of data mapper classes. It complements the corresponding theory with a decent variety of code samples.

TABLE OF CONTENTS:
  1. The Data Mapper Design Pattern: A Final Example
  2. Review: the earlier domain object and the data mapper classes
  3. Building a simple autoloader class
  4. Putting the user mapper class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 07, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To be frank, this final section of the tutorial should justify all of the effort that youíve made so far when reading the whole series. In it youíll see why data mapper classes can be so useful when building web applications that keep their domain objects isolated from the underlying storage mechanism used to persist between requests.

Now, itís example time! So, suppose for a moment that a MySQL table has been previously created, and that its schema allows you to store the first and last names of users along with their email addresses. Thatís a fairly typical scenario, isnít it? Well, look at the following script. It shows how to use the user mapper class to fetch, insert and delete users from the table in question via the mapperís interface:

<?php

try

{

    // include 'Autoloader' class

    require_once 'Autoloader.php';

   

    $db = MySQLAdapter::getInstance(array('host', 'user', 'password', 'database'));

   

    // create instance of 'UserMapper' class

    $userMapper = new UserMapper($db);

   

    // get existing user by ID

    $user = $userMapper->find(1);

    // display user data

    echo 'ID: '. $user->id . '<br />First Name: ' . $user->fname . '<br />Last Name: ' . $user->lname . '<br />Email: ' . $user->email;

   

    // create a new user and add her to the database

    $user = new User();

    $user->fname = 'Sandra';

    $user->lname = 'Smith';

    $user->email = 'sandra@domain.com';

    $userMapper->save($user);

   

    // update existing user

    $user = $userMapper->find(2);

    $user->fname = 'Sandy';

    $userMapper->save($user);

   

    // delete existing user

    $userMapper->delete($user);  

}

 

 

// catch DomainObject Exceptions

catch (DomainObjectException $e)

{

    echo $e->getMessage();

    exit();

}

 

 

// catch ClassNotFound Exceptions

catch (ClassNotFoundException $e)

{

    echo $e->getMessage();

    exit();

}

 

 

// catch MySQLAdapter Exceptions

catch (MySQLAdapterException $e)

{

    echo $e->getMessage();

    exit();

}

 

As the above example demonstrates, once an instance of the user mapper class has been created, performing CRUD operations on the fictional ďusersĒ MySQL table is a breeze. For obvious reasons, you might say that the same result can be achieved by using an Active Record implementation, and yes, youíd be right.

However, in this case the domain objects donít know anything about how to fetch, save or update themselves (the so-called persistence ignorance), as itís the responsibility of the mapper to perform these operations.

And with this final example, Iím finishing this series on using data mappers in PHP. At this time, you should be pretty familiar with the logic that stands behind implementing this database access pattern, so start building your own data mapper classes!

Final thoughts

Sad but true, weíve come to the end of this series. As you saw for yourself, in many cases the implementation of data mappers is often coupled with other data access patterns, such as an Identity Map or a Unit of Work. Such a map was used in the previous example, by the way. Units of Work, incidentally, are particularly useful for inserting, updating and deleting a group of domain objects in one go, which avoids unnecessary trips to the database. For the moment, though, I guess youíll have longs hours of fun playing with the previous data mapper classes.

See you in the next PHP tutorial!



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