public

Doctrine MasterSlaveConnection connect sets $this->_conn to the master one

  • Download Gist
MasterSlaveConnection.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.