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 22: Ev<strong>en</strong>t List<strong>en</strong>ers 313<br />

postFetch() fetch() query, data<br />

preFetchAll() fetchAll() query, data<br />

postFetchAll() fetchAll() query, data<br />

preExecute() and postExecute() only get invoked wh<strong>en</strong><br />

<strong>Doctrine</strong>_Connection::execute() is being called without prepared statem<strong>en</strong>t<br />

parameters. Otherwise <strong>Doctrine</strong>_Connection::execute() invokes prePrepare(),<br />

postPrepare(), preStmtExecute() and postStmtExecute().<br />

Hydration List<strong>en</strong>ers<br />

The hydration list<strong>en</strong>ers can be used for list<strong>en</strong>ing to resultset hydration procedures. Two<br />

methods exist for list<strong>en</strong>ing to the hydration procedure: preHydrate() and postHydrate().<br />

If you set the hydration list<strong>en</strong>er on connection level the code within the preHydrate() and<br />

postHydrate() blocks will be invoked by all compon<strong>en</strong>ts within a multi-compon<strong>en</strong>t<br />

resultset. However if you add a similar list<strong>en</strong>er on table level it only gets invoked wh<strong>en</strong> the<br />

data of that table is being hydrated.<br />

Consider we have a class called User with the following fields: first_name, last_name and<br />

age. In the following example we create a list<strong>en</strong>er that always builds a g<strong>en</strong>erated field called<br />

full_name based on first_name and last_name fields.<br />

// test.php<br />

Listing<br />

22-9<br />

// ...<br />

class HydrationList<strong>en</strong>er ext<strong>en</strong>ds <strong>Doctrine</strong>_Record_List<strong>en</strong>er<br />

{<br />

public function preHydrate(<strong>Doctrine</strong>_Ev<strong>en</strong>t $ev<strong>en</strong>t)<br />

{<br />

$data = $ev<strong>en</strong>t->data;<br />

$data['full_name'] = $data['first_name'] . ' ' .<br />

$data['last_name'];<br />

$ev<strong>en</strong>t->data = $data;<br />

}<br />

}<br />

Now all we need to do is attach this list<strong>en</strong>er to the User record and fetch some users:<br />

// test.php<br />

Listing<br />

22-10<br />

// ...<br />

$userTable = <strong>Doctrine</strong>_Core::getTable('User');<br />

$userTable->addRecordList<strong>en</strong>er(new HydrationList<strong>en</strong>er());<br />

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

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

$users = $q->execute();<br />

foreach ($users as $user) {<br />

echo $user->full_name;<br />

}<br />

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

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

Saved successfully!

Ooh no, something went wrong!