1
0
mirror of synced 2024-12-17 08:28:43 +03:00
doctrine2/manual/docs/en/working-with-objects/component-overview/manager.txt
2007-10-17 21:16:49 +00:00

60 lines
1.8 KiB
Plaintext

++++ 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>