Skip to content

Instantly share code, notes, and snippets.

@kriswallsmith
Created September 5, 2009 02:26
Show Gist options
  • Save kriswallsmith/181262 to your computer and use it in GitHub Desktop.
Save kriswallsmith/181262 to your computer and use it in GitHub Desktop.
<?php
// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
protected
$masterConnection = null,
$slaveConnection = null;
public function initializeConnections()
{
$slaves = array();
foreach (Doctrine_Manager::getInstance()->getConnections() as $name => $conn)
{
switch (true)
{
case 'master' == $name:
$this->masterConnection = $conn;
break;
case 0 === strpos($name, 'slave'):
$slaves[] = $conn;
break;
}
}
if (is_null($this->masterConnection))
{
$this->masterConnection = Doctrine_Manager::connection();
}
// choose one slave connection for the current request
$this->slaveConnection = count($slaves) ?
$slaves[rand(0, count($slaves) - 1)] :
Doctrine_Manager::connection();
}
public function getMasterConnection()
{
$this->masterConnection || $this->initializeConnections();
return $this->masterConnection;
}
public function getSlaveConnection()
{
$this->slaveConnection || $this->initializeConnections();
return $this->slaveConnection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment