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 9: DQL (<strong>Doctrine</strong> Query Language) 125<br />

// test.php<br />

Listing<br />

9-18<br />

// ...<br />

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

->select('u.username, p.*')<br />

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

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

echo $q->getSqlQuery();<br />

The above call to getSql() would output the following SQL query:<br />

SELECT<br />

u.id AS u__id,<br />

u.username AS u__username,<br />

p.id AS p__id,<br />

p.user_id AS p__user_id,<br />

p.phon<strong>en</strong>umber AS p__phon<strong>en</strong>umber<br />

FROM user u<br />

LEFT JOIN phon<strong>en</strong>umber p ON u.id = p.user_id<br />

Listing<br />

9-19<br />

The WHERE clause, if giv<strong>en</strong>, indicates the condition or conditions that the records must satisfy<br />

to be selected. where_condition is an expression that evaluates to true for each row to be<br />

selected. The statem<strong>en</strong>t selects all rows if there is no WHERE clause.<br />

// test.php<br />

Listing<br />

9-20<br />

// ...<br />

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

->select('a.name')<br />

->from('Account a')<br />

->where('a.amount > 2000');<br />

echo $q->getSqlQuery();<br />

The above call to getSql() would output the following SQL query:<br />

SELECT<br />

a.id AS a__id,<br />

a.name AS a__name<br />

FROM account a<br />

WHERE a.amount > 2000<br />

Listing<br />

9-21<br />

In the WHERE clause, you can use any of the functions and operators that DQL supports,<br />

except for aggregate (summary) functions. The HAVING clause can be used for narrowing the<br />

results with aggregate functions:<br />

// test.php<br />

Listing<br />

9-22<br />

// ...<br />

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

->select('u.username')<br />

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

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

->having('COUNT(p.id) > 3');<br />

echo $q->getSqlQuery();<br />

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

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

Saved successfully!

Ooh no, something went wrong!