Connection management - Opening a new connection.php 933 Bytes
Newer Older
1

hansbrix's avatar
hansbrix committed
2 3 4 5 6
Opening a new database connection in Doctrine is very easy. If you wish to use PDO (www.php.net/PDO) you can just initalize a new PDO object:

 

<code type="php">
zYne's avatar
zYne committed
7 8 9
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
hansbrix's avatar
hansbrix committed
10 11

try {
zYne's avatar
zYne committed
12 13 14
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
hansbrix's avatar
hansbrix committed
15
}
zYne's avatar
zYne committed
16
</code>
hansbrix's avatar
hansbrix committed
17 18 19 20 21 22 23 24



If your database extension isn't supported by PDO you can use special Doctrine_Adapter class (if availible). The following example uses db2 adapter:



<code type="php">
zYne's avatar
zYne committed
25 26 27
$dsn = 'db2:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
hansbrix's avatar
hansbrix committed
28 29

try {
zYne's avatar
zYne committed
30 31 32
    $dbh = Doctrine_Adapter::connect($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
hansbrix's avatar
hansbrix committed
33
}
zYne's avatar
zYne committed
34
</code>
hansbrix's avatar
hansbrix committed
35 36 37 38 39 40 41 42



The next step is opening a new Doctrine_Connection.



<code type="php">
zYne's avatar
zYne committed
43 44
$conn = Doctrine_Manager::connection($dbh);
</code>