Example #1
0
 protected function configureConnections()
 {
     $config = $this->container->getParameter('propel.configuration');
     $defaultConnection = !empty($config['runtime']['defaultConnection']) ? $config['runtime']['defaultConnection'] : key($config['database']['connections']);
     $serviceContainer = Propel::getServiceContainer();
     $serviceContainer->setDefaultDatasource($defaultConnection);
     foreach ($config['database']['connections'] as $name => $config) {
         if (!empty($config['slaves'])) {
             $manager = new ConnectionManagerMasterSlave();
             // configure the master (write) connection
             $manager->setWriteConfiguration($config);
             // configure the slave (read) connections
             $slaveConnections = [];
             foreach ($config['slaves'] as $slave) {
                 $slaveConnections[] = array_merge($config, ['dsn' => $slave['dsn'], 'slaves' => null]);
             }
             $manager->setReadConfiguration($slaveConnections);
         } else {
             $manager = new ConnectionManagerSingle();
             $manager->setConfiguration($config);
         }
         $serviceContainer->setAdapterClass($name, $config['adapter']);
         $serviceContainer->setConnectionManager($name, $manager);
     }
 }
 /**
  * When master is in transaction then we need to return the master connection for getReadConnection,
  * otherwise lookup queries fail
  */
 public function testReadConnectionWhenMasterIsInTransaction()
 {
     $manager = new ConnectionManagerMasterSlave();
     $manager->setWriteConfiguration(array('dsn' => 'sqlite::memory:', 'attributes' => array('ATTR_CASE' => PDO::CASE_UPPER)));
     $manager->setReadConfiguration(array(array('dsn' => 'sqlite::memory:', 'attributes' => array('ATTR_CASE' => PDO::CASE_LOWER))));
     $writeConnection = $manager->getWriteConnection(new SqliteAdapter());
     $this->assertFalse($writeConnection->inTransaction());
     $this->assertNotSame($writeConnection, $manager->getReadConnection(new SqliteAdapter()));
     $writeConnection->beginTransaction();
     $this->assertSame($writeConnection, $manager->getReadConnection(new SqliteAdapter()));
     $writeConnection->rollBack();
     $writeConnection->beginTransaction();
     $this->assertSame($writeConnection, $manager->getReadConnection(new SqliteAdapter()));
     $writeConnection->commit();
     $this->assertNotSame($writeConnection, $manager->getReadConnection(new SqliteAdapter()));
 }
Example #3
0
 /**
  * Sets the configuration for Propel and all dependencies.
  *
  * @param      mixed The Configuration (array or Configuration)
  */
 public static function setConfiguration($c)
 {
     $serviceContainer = self::getServiceContainer();
     $serviceContainer->closeConnections();
     if (is_array($c)) {
         $c = new Registry($c);
     }
     // set datasources
     if (isset($c['datasources'])) {
         foreach ($c['datasources'] as $name => $params) {
             if (!is_array($params)) {
                 continue;
             }
             // set adapters
             if (isset($params['adapter'])) {
                 $serviceContainer->setAdapterClass($name, $params['adapter']);
             }
             // set connection settings
             if (isset($params['connection'])) {
                 $conParams = $params['connection'];
                 if (isset($conParams['slaves'])) {
                     $manager = new ConnectionManagerMasterSlave();
                     $manager->setName($name);
                     $manager->setReadConfiguration($conParams['slaves']);
                     unset($conParams['slaves']);
                     $manager->setWriteConfiguration($conParams);
                 } else {
                     $manager = new ConnectionManagerSingle();
                     $manager->setName($name);
                     $manager->setConfiguration($conParams);
                 }
                 $serviceContainer->setConnectionManager($name, $manager);
             }
         }
     }
     // set default datasource
     $defaultDatasource = isset($c['datasources']['default']) ? $c['datasources']['default'] : self::DEFAULT_NAME;
     $serviceContainer->setDefaultDatasource($defaultDatasource);
     // set profiler
     if (isset($c['profiler'])) {
         $profilerConf = $c['profiler'];
         if (isset($profilerConf['class'])) {
             $serviceContainer->setProfilerClass($profilerConf['class']);
             unset($profilerConf['class']);
         }
         if ($profilerConf) {
             $serviceContainer->setProfilerConfiguration($profilerConf);
         }
     }
     // set logger
     if (isset($c['log'])) {
         foreach ($c['log'] as $loggerConfiguration) {
             $name = $loggerConfiguration['name'];
             unset($loggerConfiguration['name']);
             $serviceContainer->setLoggerConfiguration($name, $loggerConfiguration);
         }
     }
     self::$configuration = $c;
 }
Example #4
0
 public function loadConfig()
 {
     $serviceContainer = Propel::getServiceContainer();
     $database = $this->getJarves()->getSystemConfig()->getDatabase();
     if ($database->hasSlaveConnection()) {
         $manager = new ConnectionManagerMasterSlave();
         $config = $this->getManagerConfig($database->getMainConnection());
         $manager->setWriteConfiguration($config);
         $slaves = [];
         foreach ($database->getConnections() as $connection) {
             if ($connection->isSlave()) {
                 $slaves[] = $this->getManagerConfig($connection);
             }
         }
         $manager->setReadConfiguration($slaves);
     } else {
         $manager = new ConnectionManagerSingle();
         $config = $this->getManagerConfig($database->getMainConnection());
         $manager->setConfiguration($config);
     }
     $manager->setName('default');
     $serviceContainer->setAdapterClass('default', $database->getMainConnection()->getType());
     $serviceContainer->setConnectionManager('default', $manager);
     $serviceContainer->setDefaultDatasource('default');
 }
 public function testForceMasterConnectionForcesMasterConnectionOnRead()
 {
     $manager = new ConnectionManagerMasterSlave();
     $manager->setForceMasterConnection(true);
     $manager->setWriteConfiguration(array('dsn' => 'sqlite::memory:', 'attributes' => array('ATTR_CASE' => PDO::CASE_UPPER)));
     $manager->setReadConfiguration(array(array('dsn' => 'sqlite::memory:', 'attributes' => array('ATTR_CASE' => PDO::CASE_LOWER))));
     $con = $manager->getReadConnection(new SqliteAdapter());
     $pdo = $con->getWrappedConnection();
     $this->assertEquals(PDO::CASE_UPPER, $pdo->getAttribute(PDO::ATTR_CASE));
 }