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

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

->select('u.id, COUNT(t.id) AS num_threads')<br />

->from('User u, u.Threads t')<br />

->where('u.id = ?', 1)<br />

->groupBy('u.id');<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 />

COUNT(f.id) AS f__0<br />

FROM user u<br />

LEFT JOIN forum__thread f ON u.id = f.user_id<br />

WHERE u.id = ?<br />

GROUP BY u.id<br />

Listing<br />

9-29<br />

Now execute the query and inspect the results:<br />

// test.php<br />

Listing<br />

9-30<br />

// ...<br />

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

You can easily access the num_threads data with the following code:<br />

// test.php<br />

Listing<br />

9-31<br />

// ...<br />

echo $users->num_threads . ' threads found';<br />

UPDATE queries<br />

UPDATE statem<strong>en</strong>t syntax:<br />

UPDATE <br />

SET = ,<br />

= <br />

WHERE <br />

ORDER BY <br />

LIMIT <br />

Listing<br />

9-32<br />

• The UPDATE statem<strong>en</strong>t updates columns of existing records in compon<strong>en</strong>t_name<br />

with new values and returns the number of affected records.<br />

• The SET clause indicates which columns to modify and the values they should be<br />

giv<strong>en</strong>.<br />

• The optional WHERE clause specifies the conditions that id<strong>en</strong>tify which records to<br />

update. Without WHERE clause, all records are updated.<br />

• The optional ORDER BY clause specifies the order in which the records are being<br />

updated.<br />

• The LIMIT clause places a limit on the number of records that can be updated. You<br />

can use LIMIT row_count to restrict the scope of the UPDATE. A LIMIT clause is a<br />

rows-matched restriction not a rows-changed restriction. The statem<strong>en</strong>t stops as<br />

soon as it has found record_count rows that satisfy the WHERE clause, whether or<br />

not they actually were changed.<br />

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

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

Saved successfully!

Ooh no, something went wrong!