title: PHP MVC PDO Multiple databases
body: <p>I'm learning MVC in PHP and using PDO for database access.</p>
  
  <p>My database class is as follows (I use DEFINE in a config file for database variables):</p>
  
  <pre><code>class Database extends PDO {
  
      public function __construct($dbconn ="mysql") {
  
          $options = array(PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_OBJ, PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_WARNING);
  
          switch($dbconn) {
              case 'usados':
                  parent::__construct(DB_TYPE_USADOS . ':host=' . DB_HOST_USADOS . ';dbname=' . DB_NAME_USADOS, DB_USER_USADOS, DB_PASS_USADOS, $options);
                  break;
  
              case 'autos':
                  parent::__construct(DB_TYPE_AUTOS . ':host=' . DB_HOST_AUTOS . ';dbname=' . DB_NAME_AUTOS, DB_USER_AUTOS, DB_PASS_AUTOS, $options);
                  break;
  
              case 'servicos':
                  parent::__construct(DB_TYPE_SERVICOS . ':host=' . DB_HOST_SERVICOS . ';dbname=' . DB_NAME_SERVICOS, DB_USER_SERVICOS, DB_PASS_SERVICOS, $options);
                  break;
  
              default:
                  parent::__construct(DB_TYPE_MYSQL . ':host=' . DB_HOST_MYSQL . ';dbname=' . DB_NAME_MYSQL, DB_USER_MYSQL, DB_PASS_MYSQL, $options);
                  break;
          }
      }
  }
  </code></pre>
  
  <p>And in an example model, I have:</p>
  
  <pre><code>class Note_Model extends Model {
  
      public $errors = array();
  
      public function __construct() {
          parent::__construct($dbconn="mysql");
      }
  
      public function getAllNotes() {
          $sth = $this-&gt;db-&gt;prepare("SELECT user_id, note_id, note_text
                                      FROM note
                                      WHERE user_id = :user_id ;");
          $sth-&gt;execute(array(':user_id' =&gt; $_SESSION['user_id']));    
  
          return $sth-&gt;fetchAll();
      }
  }
  </code></pre>
  
  <p>I have 2 questions:</p>
  
  <ol>
  <li><p>In my database class, is my approach for different database connections (to be used by different models) ok? Is the switch structure well used for this situation and a variable with where to connect?</p></li>
  <li><p>I can connect to different databases in different models, but how would I get data from different databases in 1 same model if I had the need. Say for example, in a dashboard that shows information from different sources. It's not just $this->db, is it?</p></li>
  </ol>
  
  <p>Thank you in advanced.</p>