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 5: Connections 39<br />

Chapter 5<br />

Connections<br />

Introduction<br />

From the start <strong>Doctrine</strong> has be<strong>en</strong> designed to work with multiple connections. Unless<br />

separately specified <strong>Doctrine</strong> always uses the curr<strong>en</strong>t connection for executing the queries.<br />

In this chapter we will demonstrate how to create and work with <strong>Doctrine</strong> connections.<br />

Op<strong>en</strong>ing Connections<br />

<strong>Doctrine</strong>_Manager provides the static method <strong>Doctrine</strong>_Manager::connection()<br />

which op<strong>en</strong>s new connections.<br />

In this example we will show you to op<strong>en</strong> a new connection:<br />

// test.php<br />

Listing<br />

5-1<br />

// ...<br />

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

test', 'connection 1');<br />

Retrieve Connections<br />

If you use the <strong>Doctrine</strong>_Manager::connection() method and don't pass any argum<strong>en</strong>ts<br />

it will return the curr<strong>en</strong>t connection:<br />

// test.php<br />

Listing<br />

5-2<br />

// ...<br />

$conn2 = <strong>Doctrine</strong>_Manager::connection();<br />

if ($conn === $conn2) {<br />

echo '<strong>Doctrine</strong>_Manager::connection() returns the curr<strong>en</strong>t connection';<br />

}<br />

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

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

Saved successfully!

Ooh no, something went wrong!