Stud.IP  6.0
DBManager Class Reference

Public Member Functions

 getConnection ($database)
 
 setConnection ($database, $dsnOrConnection, $user=NULL, $pass=NULL)
 
 aliasConnection ($old, $new)
 

Static Public Member Functions

static getInstance ()
 
static get ($database='studip')
 

Detailed Description

This class provides a singleton instance that is used to manage PDO database connections.

Example of use:

# get hold of the DBManager's singleton
# set PDO connections using a DSN
$manager->setConnection('example',
'mysql:host=localhost;dbname=example',
'root', '');
# or an existing instance of PDO
$manager->setConnection('example2', $existingPdo);
# retrieve a PDO connection later in your code
$db = $manager->getConnection("studip");
# or as a shortcut
$db = DBManager::get("studip");
# or even shorter ("studip" is the default key)
# and use the connection
$db->query('SELECT * FROM user_info');
# you may even alias connections
$manager->aliasConnection("studip", "studip-slave");
# but this is just sugar for
$studip = $manager->getConnection("studip");
$manager->setConnection("studip-slave", $studip);

Member Function Documentation

◆ aliasConnection()

aliasConnection (   $old,
  $new 
)

This method creates an alias for a database connection.

This is useful if you want to use different keys but access the same database, e.g. if you want to use master-slave replication in the future

Parameters
stringthe old key of the database connection
stringthe new key of the database connection
Returns
DBManager this instance, useful for cascading method calls

◆ get()

static get (   $database = 'studip')
static

Shortcut static method to retrieve the database connection for a given key.

Example usage:

// instead of
$db = DBManager::getInstance()->getConnection("studip");
// this can be shortened to
$db = DBManager::get("studip");
// or in this case (as "studip" is the default key)
Parameters
stringthe key
Returns
StudipPDO the database connection

◆ getConnection()

getConnection (   $database)

This method returns the database connection to the given key. Throws a DBManagerException if there is no such connection.

Example usage:

try {
$db = DBManager::getInstance()->getConnection("foo");
$db->exec($sql);
} catch (DBManagerException $e) {
echo "oops";
}
Parameters
stringthe key
Exceptions
DBManagerException
Returns
PDO the database connection

◆ getInstance()

static getInstance ( )
static

This method returns the singleton instance of this class.

Returns
DBManager the singleton instance

◆ setConnection()

setConnection (   $database,
  $dsnOrConnection,
  $user = NULL,
  $pass = NULL 
)

This method maps the specified key to the specified database connection.

You can either use an instance of class PDO or specify a DSN (optionally with username/password).

The (possibly newly created) connection is configured to throw exceptions and to buffer queries if it is a MySQL connection.

Examples:

$dbManager = DBManager::getInstance();
// using an existing PDO connection
$existingPdo = new LoggingPDO($dsn);
$dbManager->setConnection('studip', $pdo);
// using a DSN with username and password
$dbManager->setConnection('studip', $dsn , $username, $password);
Parameters
stringthe key
string|PDOeither a DSN or an existing PDO connection
string(optional) the connection's username
array(optional) the connection's password
Returns
DBManager this instance, useful for cascading method calls

The documentation for this class was generated from the following file: