title: Master and slave design pattern work with transaction
body: <pre><code>&lt;?php
  
  class Database()
  {
      public function __conscruct()
      {
          $dsnMaster = 'mysql:host=' . $config['host']['master'] . ';dbname=' . $config['database'] . ';charset=utf8';
          $this-&gt;dbhMaster = new PDO($dsnMaster, $config['username'], $config['password'], $options);
          $dsnSlave = 'mysql:host=' . $config['host']['slave'] . ';dbname=' . $config['database'] . ';charset=utf8';
          $this-&gt;dbhSlave = new PDO($dsnSlave, $config['username'], $config['password'], $options);
      }
  
      public function query($query)
      {
          if (preg_match('/^select /i', $query) &gt; 0) {
              $this-&gt;dbh = $this-&gt;dbhMaster;
          } else {
              $this-&gt;dbh = $this-&gt;dbhSlave;
          }
  
          $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query);
      }
  
      public function execute()
      {
          return $this-&gt;stmt-&gt;execute();
      }
  
      public function beginTransaction()
      {
          return $this-&gt;dbh-&gt;beginTransaction();
      }
  }
  
  $db = new Database;
  
  try {
      $db-&gt;beginTransaction();
      $db-&gt;query('SELECT * FROM `tables`');
      $db-&gt;execute();
  } catch (Exception $e) {
      // rollback here
  }
  </code></pre>
  
  <p>This is how I design my pdo function with master and slave connection configs, I check query strings to use <code>SELECT</code> to master and other actions to slave, but the transaction will be failed because I didn't declare <code>$this-&gt;dbh</code> in the <code>__construct</code>, how to fix that?</p>