manager.txt 1.79 KB
Newer Older
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 51 52 53 54 55 56 57 58 59
++++ Introduction

{{Doctrine_Manager}} is the heart of every Doctrine based application. {{Doctrine_Manager}} handles all connections (database connections).


++++ Opening a new connection

In order to get your first application started you first need to get an instance of Doctrine_Manager which handles all the connections (database connections). The second thing to do is to open a new connection. 

<code type="php">
// Doctrine_Manager controls all the connections 

$manager = Doctrine_Manager::getInstance();

// Doctrine_Connection
// a script may have multiple open connections
// (= multiple database connections)
$dbh  = new PDO('dsn','username','password');
$conn = $manager->openConnection();

// or if you want to use Doctrine Doctrine_Db and its 
// performance monitoring capabilities

$dsn  = 'schema://username:password@dsn/dbname';
$dbh  = Doctrine_Db::getConnection($dsn);
$conn = $manager->openConnection();
</code>


++++ Managing connections

Switching between connections in Doctrine is very easy, you just call {{Doctrine_Manager::setCurrentConnection()}} method. You can access the connection by calling {{Doctrine_Manager::getConnection()}} or {{Doctrine_Manager::getCurrentConnection()}} if you only want to get the current connection.

<code type="php">
// Doctrine_Manager controls all the connections 

$manager = Doctrine_Manager::getInstance();

// open first connection

$conn = $manager->openConnection(new PDO('dsn','username','password'), 'connection 1');

// open second connection

$conn2 = $manager->openConnection(new PDO('dsn2','username2','password2'), 'connection 2');

$manager->getCurrentConnection(); // $conn2

$manager->setCurrentConnection('connection 1');

$manager->getCurrentConnection(); // $conn

// iterating through connections

foreach($manager as $conn) {
    
}
</code>