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 22: Ev<strong>en</strong>t List<strong>en</strong>ers 318<br />

Listing<br />

22-19<br />

All of the above methods in the list<strong>en</strong>er could optionally be placed in the user class below.<br />

<strong>Doctrine</strong> will check there for the hooks as well:<br />

class User ext<strong>en</strong>ds <strong>Doctrine</strong>_Record<br />

{<br />

// ...<br />

public function preDqlSelect()<br />

{<br />

// ...<br />

}<br />

public function preDqlUpdate()<br />

{<br />

// ...<br />

}<br />

}<br />

public function preDqlDelete()<br />

{<br />

// ...<br />

}<br />

In order for these dql callbacks to be checked, you must explicitly turn them on. Because this<br />

adds a small amount of overhead for each query, we have it off by default. We already<br />

<strong>en</strong>abled this attribute in an earlier chapter.<br />

Here it is again to refresh your memory:<br />

Listing<br />

22-20<br />

// bootstrap.php<br />

Listing<br />

22-21<br />

// ...<br />

$manager->setAttribute(<strong>Doctrine</strong>_Core::ATTR_USE_DQL_CALLBACKS, true);<br />

Now wh<strong>en</strong> you interact with the User model it will take in to account the deleted flag:<br />

Delete user through record instance:<br />

$user = new User();<br />

$user->username = 'jwage';<br />

$user->password = 'changeme';<br />

$user->save();<br />

$user->delete();<br />

The above call to $user->delete() does not actually delete the record instead it sets the<br />

deleted flag to true.<br />

Listing<br />

22-22<br />

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

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

Listing<br />

22-23<br />

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

SELECT<br />

u.id AS u__id,<br />

u.username AS u__username,<br />

u.password AS u__password,<br />

u.deleted AS u__deleted<br />

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

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

Saved successfully!

Ooh no, something went wrong!