Skip to content

Instantly share code, notes, and snippets.

@jonathaningram
Created August 31, 2012 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathaningram/3547750 to your computer and use it in GitHub Desktop.
Save jonathaningram/3547750 to your computer and use it in GitHub Desktop.
Doctrine MasterSlaveConnection connect sets $this->_conn to the master one
<?php
class MasterSlaveConnection
{
public function connect($connectionName = 'slave')
{
if ( $connectionName !== 'slave' && $connectionName !== 'master' ) {
throw new \InvalidArgumentException("Invalid option to connect(), only master or slave allowed.");
}
$forceMasterAsSlave = false;
if ($this->getTransactionNestingLevel() > 0) {
$connectionName = 'master';
$forceMasterAsSlave = true;
}
if ($this->connections[$connectionName]) {
if ($forceMasterAsSlave) {
$this->connections['slave'] = $this->_conn = $this->connections['master'];
} else {
// **** this is the connection that is set up ***
$this->_conn = $this->connections[$connectionName];
}
return false;
}
if ($connectionName === 'master') {
/** Set slave connection to master to avoid invalid reads */
if ($this->connections['slave'] && ! $this->keepSlave) {
unset($this->connections['slave']);
}
$this->connections['master'] = $this->_conn = $this->connectTo($connectionName);
if ( ! $this->keepSlave) {
$this->connections['slave'] = $this->connections['master'];
}
} else {
$this->connections['slave'] = $this->_conn = $this->connectTo($connectionName);
}
if ($this->_eventManager->hasListeners(Events::postConnect)) {
$eventArgs = new ConnectionEventArgs($this);
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment