Skip to content

Instantly share code, notes, and snippets.

@muskie9
Created June 27, 2019 00:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save muskie9/205e6dbbcb79d69734aa1b0b3c5087bc to your computer and use it in GitHub Desktop.
Save muskie9/205e6dbbcb79d69734aa1b0b3c5087bc to your computer and use it in GitHub Desktop.
<?php
namespace Your\Namespace;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\ORM\DB;
/**
* Class ConnectionManager
* @package Your\Namespace
*/
class ConnectionManager
{
use Configurable;
/**
* @var array
*/
private static $alternate = [
'type' => 'MySQLDatabase',
'server' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'database',
];
/**
*
*/
public static function connect_default()
{
DB::connect(DB::getConfig('default'));
}
/**
*
*/
public static function connect_alternate()
{
if (!DB::getConfig('alternate')) {
DB::setConfig(static::config()->get('alternate'), 'alternate');
}
DB::connect(DB::getConfig('alternate'));
}
}
@muskie9
Copy link
Author

muskie9 commented Jun 27, 2019

Use Case

use Your\Namespace\ConnectionManager;

class ...
/** your code */
ConnectionManager::connect_alternate();

$query = SQLSelect::create();
// build and execute your query to the non-SS database

ConnectionManager::connect_default();

/** end your code */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment