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 110<br />

Now consider the DQL equival<strong>en</strong>t of the SQL query we used:<br />

Listing<br />

8-38<br />

// test.php<br />

// ...<br />

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

->select('u.id, u.username, p.phon<strong>en</strong>umber')<br />

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

->leftJoin('u.Phon<strong>en</strong>umbers p');<br />

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

print_r($results);<br />

The structure of this hydrated array would look like:<br />

Listing<br />

8-39<br />

$ php test.php<br />

Array<br />

(<br />

[0] => Array<br />

(<br />

[id] => 1<br />

[username] =><br />

[Phon<strong>en</strong>umbers] => Array<br />

(<br />

[0] => Array<br />

(<br />

[id] => 1<br />

[phon<strong>en</strong>umber] => 123 123<br />

)<br />

[1] => Array<br />

(<br />

[id] => 2<br />

[phon<strong>en</strong>umber] => 456 123<br />

)<br />

[2] => Array<br />

(<br />

[id] => 3<br />

[phon<strong>en</strong>umber] => 123 777<br />

)<br />

)<br />

)<br />

)<br />

// ...<br />

This structure also applies to the hydration of objects(records) which is the default hydration<br />

mode of <strong>Doctrine</strong>. The only differ<strong>en</strong>ces are that the individual elem<strong>en</strong>ts are repres<strong>en</strong>ted as<br />

<strong>Doctrine</strong>_Record objects and the arrays converted into <strong>Doctrine</strong>_Collection objects.<br />

Whether dealing with arrays or objects you can:<br />

1. Iterate over the results using foreach<br />

2. Access individual elem<strong>en</strong>ts using array access brackets<br />

3. Get the number of elem<strong>en</strong>ts using count() function<br />

4. Check if giv<strong>en</strong> elem<strong>en</strong>t exists using isset()<br />

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

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

Saved successfully!

Ooh no, something went wrong!