Created
September 5, 2009 02:54
-
-
Save kriswallsmith/181270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ConnectionListener extends Doctrine_Connection | |
implements Doctrine_EventListener_Interface | |
{ | |
protected | |
$master = null, | |
$slave = null; | |
public function __construct(PDO $master, PDO $slave) | |
{ | |
$this->master = $master; | |
$this->slave = $slave; | |
} | |
public function preQuery(Doctrine_Event $event) | |
{ | |
$this->forceDbh($event->getInvoker(), 'slave'); | |
} | |
public function postQuery(Doctrine_Event $event) | |
{ | |
$this->restoreDbh($event->getInvoker()); | |
} | |
public function prePrepare(Doctrine_Event $event) | |
{ | |
$use = 0 === strpos(trim(strtolower($event->getQuery())), 'select') ? | |
'slave' : 'master'; | |
$this->forceDbh($event->getInvoker(), $use); | |
} | |
public function postStmtExecute(Doctrine_Event $event) | |
{ | |
$this->restoreDbh($event->getInvoker()->getConnection()); | |
} | |
public function preExec(Doctrine_Event $event) | |
{ | |
$this->forceDbh($event->getInvoker(), 'master'); | |
} | |
public function postExec(Doctrine_Event $event) | |
{ | |
$this->restoreDbh($event->getInvoker()); | |
} | |
// protected | |
protected function forceDbh($conn, $type) | |
{ | |
if ($this->$type !== $conn->dbh) | |
{ | |
$conn->options['previous_dbh'] = $conn->dbh; | |
$conn->dbh = $this->$type; | |
} | |
} | |
protected function restoreDbh($conn) | |
{ | |
if (isset($conn->options['previous_dbh'])) | |
{ | |
$conn->dbh = $conn->options['previous_dbh']; | |
unset($conn->options['previous_dbh']); | |
} | |
} | |
// the remaining methods required by Doctrine_EventListener_Interface | |
public function preTransactionCommit(Doctrine_Event $event) { } | |
public function postTransactionCommit(Doctrine_Event $event) { } | |
public function preTransactionRollback(Doctrine_Event $event) { } | |
public function postTransactionRollback(Doctrine_Event $event) { } | |
public function preTransactionBegin(Doctrine_Event $event) { } | |
public function postTransactionBegin(Doctrine_Event $event) { } | |
public function postConnect(Doctrine_Event $event) { } | |
public function preConnect(Doctrine_Event $event) { } | |
public function postPrepare(Doctrine_Event $event) { } | |
public function preStmtExecute(Doctrine_Event $event) { } | |
public function preError(Doctrine_Event $event) { } | |
public function postError(Doctrine_Event $event) { } | |
public function preFetch(Doctrine_Event $event) { } | |
public function postFetch(Doctrine_Event $event) { } | |
public function preFetchAll(Doctrine_Event $event) { } | |
public function postFetchAll(Doctrine_Event $event) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment