Connection management - Lazy-connecting to database.php 838 Bytes
Newer Older
1

hansbrix's avatar
hansbrix committed
2 3 4 5 6 7 8 9 10 11 12
Lazy-connecting to database is handled via Doctrine_Db wrapper. When using Doctrine_Db instead of PDO / Doctrine_Adapter, lazy-connecting 
to database is being performed (that means Doctrine will only connect to database when needed). 

This feature can be very useful
when using for example page caching, hence not actually needing a database connection on every request. Remember connecting to database is an expensive operation.

 

<code type="php">
// we may use PDO / PEAR like DSN
// here we use PEAR like DSN
zYne's avatar
zYne committed
13
$dbh = new Doctrine_Db('mysql://username:password@localhost/test');
hansbrix's avatar
hansbrix committed
14 15 16
// !! no actual database connection yet !!

// initalize a new Doctrine_Connection
zYne's avatar
zYne committed
17
$conn = Doctrine_Manager::connection($dbh);
hansbrix's avatar
hansbrix committed
18 19 20
// !! no actual database connection yet !!

// connects database and performs a query
zYne's avatar
zYne committed
21
$conn->query('FROM User u');
zYne's avatar
zYne committed
22
</code>