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!
blog comments powered by Disqus |
|
|
|
|
|
|
|