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

Enabling the user mapper class to delete users - PHP

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



True to form, giving the previous "UserMapper" class the ability to delete users from its associated MySQL table is only a matter of coding a method that internally performs a DELETE statement via the corresponding MySQL driver and nothing else.

While this description sounds comprehensive, the best way to understand the logic that drives this method is by showing its entire implementation. That's exactly what the code fragment below does:

// delete domain object

public function delete(DomainObjectAbstract $user)


    if ($user->id !== NULL)


        $this->_db->query("DELETE FROM $this->_table WHERE id = $user->id");



As you can see, the brand new "delete()" method takes a user domain object as an incoming argument, whose ID is used to perform the actual deletion operation via the MySQL adapter. I've seen some alternative implementations of a method like this that also directly accepts the ID of the object to be deleted, but adding this functionality is only a matter of personal preference. For the sake of brevity, the method will be kept simple.

All in all, now that the user mapper class has been given the capability to delete users through a clean interface, it's time to show its full source code, so you'll be able to see how this class looks after adding the "delete()" method to it that you saw before.

This will be done in the last segment of this tutorial. So click on the link below and read the next few lines.

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