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) 146<br />

Listing<br />

9-115<br />

which the search is started (by default, the beginning of the string to be searched). The first<br />

position in a string is d<strong>en</strong>oted by 1. If the string is not found, 0 is returned.<br />

The LENGTH function returns the l<strong>en</strong>gth of the string in characters as an integer.<br />

Arithmetic functions<br />

Availible DQL arithmetic functions:<br />

ABS(simple_arithmetic_expression)<br />

SQRT(simple_arithmetic_expression)<br />

MOD(simple_arithmetic_expression, simple_arithmetic_expression)<br />

• The ABS function returns the absolute value for giv<strong>en</strong> number.<br />

• The SQRT function returns the square root for giv<strong>en</strong> number.<br />

• The MOD function returns the modulus of first argum<strong>en</strong>t using the second<br />

argum<strong>en</strong>t.<br />

Subqueries<br />

Introduction<br />

<strong>Doctrine</strong> allows you to use sub-dql queries in the FROM, SELECT and WHERE statem<strong>en</strong>ts.<br />

Below you will find examples for all the differ<strong>en</strong>t types of subqueries <strong>Doctrine</strong> supports.<br />

Comparisons using subqueries<br />

Find all the users which are not in a specific group.<br />

Listing<br />

9-116<br />

// test.php<br />

Listing<br />

9-117<br />

// ...<br />

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

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

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

->where('u.id NOT IN (SELECT u.id FROM User u INNER JOIN u.Groups g<br />

WHERE g.id = ?)', 1);<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 />

FROM user u<br />

WHERE u.id NOT IN (SELECT<br />

u2.id AS u2__id<br />

FROM user u2<br />

INNER JOIN user_group u3 ON u2.id = u3.user_id<br />

INNER JOIN groups g ON g.id = u3.group_id<br />

WHERE g.id = ?)<br />

Retrieve the users phon<strong>en</strong>umber in a subquery and include it in the resultset of user<br />

information.<br />

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

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

Saved successfully!

Ooh no, something went wrong!