title: Is my PHP controller structured the wrong way? Code in the wrong place
body: <p>I've fairly new to PHP MVC and I'm not 100% sold I'm doing this in the most logical way possible. This is a piece of code from one of my controllers. A friend said a majority of this code should be moved into the Model? Is this so?</p>
  
  <p>Right now I have a DataAccess layer that does all the SQL work. For User work I have a UserDataAccess object, which will create, update and delete. It appears I'm using something along the lines of the Data Mapper pattern. My Model classes map directly to the database columns on a given table and that's it. </p>
  
  <p>I'm continually confused at where each piece of code should go.</p>
  
  <pre><code>// Posted values passed all checks.
          if(count($errors) == 0)
          {                   
              try
              {
                  // Get a Connection.
                  $connection = ConnectionFactory::GetConnection('mysql');
  
                  try
                  {
                      $connection-&gt;beginTransaction();                
  
                      // Create DataAccess object.
                      $uda = new UserDataAccess();
  
                      // Check if username and email are avail.                       
                      if(count($uda-&gt;CheckUsername($connection, $user-&gt;Username)) &gt; 0)
                          $errors['username_taken'] = "Username is taken";
                      if(count($uda-&gt;CheckEmail($connection, $user-&gt;Email)) &gt; 0)
                          $errors['email_taken'] = "Email is taken";      
  
                      if(count($errors) == 0)
                      {
                          // Perform Create.
                          $userId = $uda-&gt;CreateUser($connection, $user-&gt;GetArray());
                          // Add User to Rookie Role.             
                          $uda-&gt;AddUserToRole($connection, 1, $userId);   
                          // Commit connection.
                          $connection-&gt;commit();
                      }
  
                      // Clear connection.
                      $connection = null;
  
                      // Redirect user.   
                      if(count($errors) == 0)
                          header('location: /' . $user-&gt;Username);
                  }
                  catch(PDOException $e)
                  {
                      $connection-&gt;rollBack();
                      $errors['internal'] = "Opps: There was an error processing the request.";
                  }
              }
              catch(Exception $e)
              {
                  $errors['internal'] = "Opps: There was an error processing the request.";   
              }                               
          }   
      }
  </code></pre>