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

Listing<br />

9-33<br />

// test.php<br />

// ...<br />

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

->update('Account')<br />

->set('amount', 'amount + 200')<br />

->where('id > 200');<br />

// If you just want to set the amount to a value<br />

$q->set('amount', '?', 500);<br />

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

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

Listing<br />

9-34<br />

UPDATE account<br />

SET amount = amount + 200<br />

WHERE id > 200<br />

Now to perform the update is simple. Just execute the query:<br />

Listing<br />

9-35<br />

// test.php<br />

// ...<br />

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

echo $rows;<br />

DELETE Queries<br />

Listing<br />

9-36<br />

DELETE<br />

FROM <br />

WHERE <br />

ORDER BY <br />

LIMIT <br />

• The DELETE statem<strong>en</strong>t deletes records from compon<strong>en</strong>t_name and returns the<br />

number of records deleted.<br />

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

delete. Without WHERE clause, all records are deleted.<br />

• If the ORDER BY clause is specified, the records are deleted in the order that is<br />

specified.<br />

• The LIMIT clause places a limit on the number of rows that can be deleted. The<br />

statem<strong>en</strong>t will stop as soon as it has deleted record_count records.<br />

Listing<br />

9-37<br />

// test.php<br />

// ...<br />

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

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

->where('a.id > 3');<br />

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

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

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

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

Saved successfully!

Ooh no, something went wrong!