1
0
mirror of synced 2024-12-14 07:06:04 +03:00
doctrine2/manual/docs/Connection management - Opening a new connection.php

45 lines
933 B
PHP
Raw Normal View History

2007-04-14 01:49:11 +04:00
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">
2007-05-07 19:44:20 +04:00
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
2007-04-14 01:49:11 +04:00
try {
2007-05-07 19:44:20 +04:00
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
2007-04-14 01:49:11 +04:00
}
2007-05-07 19:44:20 +04:00
</code>
2007-04-14 01:49:11 +04:00
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">
2007-05-07 19:44:20 +04:00
$dsn = 'db2:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
2007-04-14 01:49:11 +04:00
try {
2007-05-07 19:44:20 +04:00
$dbh = Doctrine_Adapter::connect($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
2007-04-14 01:49:11 +04:00
}
2007-05-07 19:44:20 +04:00
</code>
2007-04-14 01:49:11 +04:00
The next step is opening a new Doctrine_Connection.
<code type="php">
2007-05-07 19:44:20 +04:00
$conn = Doctrine_Manager::connection($dbh);
</code>