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