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

->from('Forum_Thread t')<br />

->orderby('rand')<br />

->limit(1);<br />

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

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

SELECT<br />

f.id AS f__id,<br />

RAND() AS f__0<br />

FROM forum__thread f<br />

ORDER BY f__0<br />

LIMIT 1<br />

Listing<br />

9-138<br />

LIMIT and OFFSET clauses<br />

Propably the most complex feature DQL parser has to offer is its LIMIT clause parser. Not<br />

only does the DQL LIMIT clause parser take care of LIMIT database portability it is capable<br />

of limiting the number of records instead of rows by using complex query analysis and<br />

subqueries.<br />

Retrieve the first 20 users and all their associated phon<strong>en</strong>umbers:<br />

// test.php<br />

Listing<br />

9-139<br />

// ...<br />

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

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

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

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

->limit(20);<br />

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

You can also use the offset() method of the <strong>Doctrine</strong>_Query object in<br />

combination with the limit() method to produce your desired LIMIT and<br />

OFFSET in the executed SQL query.<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.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 />

LIMIT 20<br />

Listing<br />

9-140<br />

Driver Portability<br />

DQL LIMIT clause is portable on all supported databases. Special att<strong>en</strong>tion have be<strong>en</strong> paid to<br />

following facts:<br />

• Only Mysql, Pgsql and Sqlite implem<strong>en</strong>t LIMIT / OFFSET clauses natively<br />

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

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

Saved successfully!

Ooh no, something went wrong!