10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 28: Improving Performance 366<br />

Listing<br />

28-14<br />

Listing<br />

28-15<br />

memory unless you <strong>manual</strong>ly break the circular refer<strong>en</strong>ce chains. <strong>Doctrine</strong> provides a free()<br />

function on <strong>Doctrine</strong>_Record, <strong>Doctrine</strong>_Collection, and <strong>Doctrine</strong>_Query which<br />

eliminates the circular refer<strong>en</strong>ces on those objects, freeing them up for garbage collection.<br />

Usage might look like:<br />

Free objects wh<strong>en</strong> mass inserting records:<br />

for ($i = 0; $i < 1000; $i++)<br />

{<br />

$object = createBigObject();<br />

$object->save();<br />

$object->free(true);<br />

}<br />

You can also free query objects in the same way:<br />

for ($i = 0; $i < 1000; $i++)<br />

{<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

->from('User u');<br />

}<br />

$results = $q->fetchArray();<br />

$q->free();<br />

Or ev<strong>en</strong> better if you can reuse the same query object for each query in the loop that would<br />

be ideal:<br />

Listing<br />

28-16<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

->from('User u');<br />

for ($i = 0; $i < 1000; $i++)<br />

{<br />

$results = $q->fetchArray();<br />

$q->free();<br />

}<br />

Other Tips<br />

Helping the DQL parser<br />

There are two possible ways wh<strong>en</strong> it comes to using DQL. The first one is writing the plain<br />

DQL queries and passing them to <strong>Doctrine</strong>_Connection::query($dql). The second one<br />

is to use a <strong>Doctrine</strong>_Query object and its flu<strong>en</strong>t interface. The latter should be preferred for<br />

all but very simple queries. The reason is that using the <strong>Doctrine</strong>_Query object and it's<br />

methods makes the life of the DQL parser a little bit easier. It reduces the amount of query<br />

parsing that needs to be done and is therefore faster.<br />

Effici<strong>en</strong>t relation handling<br />

Wh<strong>en</strong> you want to add a relation betwe<strong>en</strong> two compon<strong>en</strong>ts you should NOT do something like<br />

the following:<br />

The following example assumes a many-many betwe<strong>en</strong> Role and User.<br />

Listing<br />

28-17<br />

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

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

Saved successfully!

Ooh no, something went wrong!