title: Datamapper DB Pattern With ZF: Managing a Relational Database
body: <p>I am creating an application using Zend Framework and I want to use the data mapper design pattern for managing my database. What I don't understand is how to use the pattern with a relational database.</p>
  
  <p>Suppose I have a table containing posts with columns id, title, text and author. I could use the following classes to manage this:</p>
  
  <pre><code>class Application_Model_DbTable_Posts extends Zend_Db_Table_Abstract
  {
      $_name = 'posts';
  }
  
  class Application_Model_PostMapper
  {
      public function setDbTable($dbTable);
      public function getDbTable();
      public function save($post);
      public function find($id);
      public function fetchAll();
  }
  
  class Application_Model_Post
  {
      protected $_id;
      protected $_title;
      protected $_text;
      protected $_author;
  
      public function __set($name, $value);
      public function __get($name);
  }
  </code></pre>
  
  <p>Now, suppose I wanted to create a one-to-many relationship -- comments, for example -- how would I do this? And a many-to-many relationship? -- or would that be done the same was as a one-to-many?</p>
  
  <p>Please provide code samples in your answers if possible, in order to help me understand.</p>