title: Transactional PDO with MVC across multiple models
body: <p>I'm building a site and have multiple segments of code, across multiple models, that needs to run within a transaction so if one fails, the code will roll back.</p>
  
  <p>Say I have a simple form to register a user.</p>
  
  <pre><code>&lt;form action="/register" method="POST"&gt;
          &lt;div&gt;
              &lt;label for="username"&gt;Username&lt;/label&gt;
              &lt;input type="text" name="username" id="username" /&gt;         
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label for="username"&gt;Password&lt;/label&gt;
              &lt;input type="password" name="password" id="password" /&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label for="username"&gt;Email&lt;/label&gt;
              &lt;input type="text" name="email" id="email" /&gt;   
          &lt;/div&gt;
          &lt;div&gt;&lt;input type="submit" name="submit" value="submit" /&gt;&lt;/div&gt;
      &lt;/form&gt; 
  </code></pre>
  
  <p>In my system when a user is created, I need to automatically place them in a role.</p>
  
  <p>To insert into the User table, I use my User model. Then I need to insert the Role table which occurs in a separate model. If all work needed to be done lies within separate models, where do I create the connection to be passed across multiple models to allow the transaction to work? </p>
  
  <pre><code>// Start Transaction.
  // Create new user based on posted variables. UserModel
  // Add user to a given role. UserRoleModel -&gt; Table contains UserId and RoleId
  // Commit transaction.
  </code></pre>
  
  <p>Maybe where I am confused is, should all work to create a user be in my user model? Even if the work spans across more than just the User db table? My assumption is that each table in the database should have a model class and that model class should do only work within that table? Am I wrong?</p>
  
  <p>Thanks  </p>