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 14: Data Hydrators 230<br />

Listing<br />

14-11<br />

Writing Hydration Method<br />

<strong>Doctrine</strong> offers the ability to write your own hydration methods and register them with<br />

<strong>Doctrine</strong> for use. All you need to do is write a class that ext<strong>en</strong>ds<br />

<strong>Doctrine</strong>_Hydrator_Abstract and register it with <strong>Doctrine</strong>_Manager.<br />

First lets write a sample hydrator class:<br />

class <strong>Doctrine</strong>_Hydrator_MyHydrator ext<strong>en</strong>ds <strong>Doctrine</strong>_Hydrator_Abstract<br />

{<br />

public function hydrateResultSet($stmt)<br />

{<br />

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);<br />

// do something to with $data<br />

return $data;<br />

}<br />

}<br />

To use it make sure we register it with <strong>Doctrine</strong>_Manager:<br />

Listing<br />

14-12<br />

// bootstrap.php<br />

// ...<br />

$manager->registerHydrator('my_hydrator', '<strong>Doctrine</strong>_Hydrator_MyHydrator');<br />

Now wh<strong>en</strong> you execute your queries you can pass my_hydrator and it will use your class to<br />

hydrate the data.<br />

Listing<br />

14-13<br />

$q->execute(array(), 'my_hydrator');<br />

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

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

Saved successfully!

Ooh no, something went wrong!