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 8: Working with Models 116<br />

->leftJoin('u.Groups g')<br />

->where('g.name != ?', 'Group 2');<br />

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

<strong>Doctrine</strong> has many differ<strong>en</strong>t ways you can execute queries and retrieve the data. Below are<br />

examples of all the differ<strong>en</strong>t ways you can execute a query:<br />

First lets create a sample query to test with:<br />

Listing<br />

8-63<br />

// test.php<br />

// ...<br />

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

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

You can use array hydration with the fetchArray() method:<br />

Listing<br />

8-64<br />

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

You can also use array hydration by specifying the hydration method to the second<br />

argum<strong>en</strong>t of the execute() method:<br />

Listing<br />

8-65<br />

// test.php<br />

// ...<br />

$users = $q->execute(array(), <strong>Doctrine</strong>_Core::HYDRATE_ARRAY)<br />

You can also specify the hydration method by using the setHydrationMethod()<br />

method:<br />

Listing<br />

8-66<br />

$users = $q->setHydrationMode(<strong>Doctrine</strong>_Core::HYDRATE_ARRAY)->execute(); //<br />

So is this<br />

Custom accessors and mutators will not work wh<strong>en</strong> hydrating data as anything except<br />

records. Wh<strong>en</strong> you hydrate as an array it is only a static array of data and is not object<br />

ori<strong>en</strong>ted. If you need to add custom values to your hydrated arrays you can use the some of<br />

the ev<strong>en</strong>ts such as preHydrate and postHydrate<br />

Sometimes you may want to totally bypass hydration and return the raw data that<br />

PDO returns:<br />

Listing<br />

8-67<br />

// test.php<br />

// ...<br />

$users = $q->execute(array(), <strong>Doctrine</strong>_Core::HYDRATE_NONE);<br />

More can be read about skipping hydration in the improving performance (page 362)<br />

chapter.<br />

If you want to just fetch one record from the query:<br />

Listing<br />

8-68<br />

// test.php<br />

// ...<br />

$user = $q->fetchOne();<br />

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

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

Saved successfully!

Ooh no, something went wrong!