10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Introduction to Connections 28<br />

// ...<br />

$conn->setOption('username', $user);<br />

$conn->setOption('password', $password);<br />

Lazy Database Connecting<br />

Lazy-connecting to database can save a lot of resources. There might be many times where<br />

you don't need an actual database connection, h<strong>en</strong>ce its always recomm<strong>en</strong>ded to use lazyconnecting<br />

(that means <strong>Doctrine</strong> will only connect to database wh<strong>en</strong> needed).<br />

This feature can be very useful wh<strong>en</strong> using for example page caching, h<strong>en</strong>ce not actually<br />

needing a database connection on every request. Remember connecting to database is an<br />

exp<strong>en</strong>sive operation.<br />

In the example below we will show you wh<strong>en</strong> you create a new <strong>Doctrine</strong> connection, the<br />

connection to the database isn't created until it is actually needed.<br />

Listing<br />

3-11<br />

// bootstrap.php<br />

// ...<br />

// At this point no actual connection to the database is created<br />

$conn = <strong>Doctrine</strong>_Manager::connection('mysql://username:password@localhost/<br />

test');<br />

// The first time the connection is needed, it is instantiated<br />

// This query triggers the connection to be created<br />

$conn->execute('SHOW TABLES');<br />

Testing your Connection<br />

After reading the previous sections of this chapter, you should now know how to create a<br />

connection. So, lets modify our bootstrap file to include the initialization of a connection. For<br />

this example we will just be using a sqlite memory database but you can use whatever type of<br />

database connection you prefer.<br />

Add your database connection to bootstrap.php and it should look something like the<br />

following:<br />

Listing<br />

3-12<br />

/**<br />

* Bootstrap <strong>Doctrine</strong>.php, register autoloader and specify<br />

* configuration attributes<br />

*/<br />

require_once('../doctrine/branches/1.0/lib/<strong>Doctrine</strong>.php');<br />

spl_autoload_register(array('<strong>Doctrine</strong>', 'autoload'));<br />

$manager = <strong>Doctrine</strong>_Manager::getInstance();<br />

$conn = <strong>Doctrine</strong>_Manager::connection('sqlite::memory:', 'doctrine');<br />

To test the connection lets modify our test.php script and perform a small test. Since we<br />

create a variable name $conn, that variable is available to the test script so lets setup a small<br />

test to make sure our connection is working:<br />

First lets create a test table and insert a record:<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!