title: PHP how to implement the Data Mapper pattern in MVC
body: <p>I'm trying to implement and get my grasp on the Data Mapper pattern in my MVC project. I know this has been asked before, but I'm still not sure if I'm moving in the right direction. I've pasted my Controller, Datamapper and Domain Object/Entity code right below:</p>
  
  <h1>Controller</h1>
  
  <p>The user lands on mywebsite.com/dashboard/index and gets to see a list of events from the event log database table. The index() function creates an Datamapper instance and passes a the database wrapper. Then an HTML table is dynamically generated with data fetched from the Data mapper.</p>
  
  <pre><code>class DashboardController extends BaseController { 
      public function index() {
          require_once '../app/domain/mappers/EventMapper.php';
          require_once '../app/include/HTMLTableCreator.php';
  
          try {
              $eventMapper = new EventMapper(new Database('db'));
              $table = new HTMLTableCreator($eventMapper-&gt;findByLimit(20));
          } catch (DatabaseException $e) {
  
          }
  
          $this-&gt;view('/home/dashboard', ['table' =&gt; $table-&gt;toString()]);
      }   
  }
  </code></pre>
  
  <h1>Datamapper</h1>
  
  <p>This class contains the findByLimit() method for retrieving the last 20 log events from the database. The fetchCollection() method populates the Domain Entities and collects these in an array to be returned to the controller.</p>
  
  <p>Should I create a generic collection function to be used by multiple Data mappers when the SQL query returns multiple rows (= multiple domain objects)? And can a Data mapper use any type of Data Source?</p>
  
  <pre><code>require_once '../app/domain/entities/Event.php';
  require_once '../app/domain/interfaces/EventMapperInterface.php';
  
  /**
   * EventMapper class
   */
  class EventMapper implements EventMapperInterface {
  
      protected $dbh;
      protected $elements;
  
      public function __construct(Database $dbh) {
          $this-&gt;dbh = $dbh;
      }
  
      public function fetchCollection($objects) {
          foreach($objects as $object) {
              $event = new Event();
              $event-&gt;setId($object['id']);
              $event-&gt;setDateTime($object['datetime']);
              $event-&gt;setMessage($object['message']);
              $event-&gt;setHostname($object['hostname']);
  
              $this-&gt;elements[] = $event;
          }
  
          return $this-&gt;elements;
      }
  
      public function findByLimit($limit = 20) {
          $events = $this-&gt;dbh-&gt;sql_select('SELECT * FROM eventlog LIMIT ' . $limit, []);
  
          return $this-&gt;fetchCollection($events);
      }   
  }
  </code></pre>
  
  <h1>Domain Object/Entity</h1>
  
  <p>At last the domain object with some simple getters and setters. Would this be the place to do validation on the properties of a Domain Object?</p>
  
  <pre><code>class Event {
  
      protected $id;
      protected $dateTime;
      protected $message;
      protected $hostname;
  
      public function setId($id) {
          $this-&gt;id = $id;
      }
  
      public function getId() {
          return $this-&gt;id;
      }
  
      public function setDateTime($dateTime) {
          $this-&gt;dateTime = $dateTime;
      }
  
      public function getDateTime() {
          return $this-&gt;dateTime;
      }  
  
      // rest omitted for clarity
  }
  </code></pre>
  
  <p>Am I missing something in my implementation?</p>