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 8: Working with Models 114<br />

Perform DQL delete with simple WHERE:<br />

Listing<br />

8-52<br />

// test.php<br />

// ...<br />

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

->delete('Phon<strong>en</strong>umber')<br />

->addWhere('user_id = 5');<br />

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

Perform simple DQL update for a column:<br />

Listing<br />

8-53<br />

// test.php<br />

// ...<br />

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

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

->set('u.is_active', '?', true)<br />

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

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

Perform DQL update with DBMS function. Make all usernames lowercase:<br />

Listing<br />

8-54<br />

// test.php<br />

// ...<br />

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

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

->set('u.username', 'LOWER(u.username)');<br />

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

Using mysql LIKE to search for records:<br />

Listing<br />

8-55<br />

// test.php<br />

// ...<br />

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

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

->where('u.username LIKE ?', '%jwage%');<br />

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

Use the INDEXBY keyword to hydrate the data where the key of record <strong>en</strong>try is the<br />

name of the column you assign:<br />

Listing<br />

8-56<br />

// test.php<br />

// ...<br />

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

->from('User u INDEXBY u.username');<br />

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

Now we can print the user with the username of jwage:<br />

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

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

Saved successfully!

Ooh no, something went wrong!