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

Updating Related Records<br />

You can update the related records by calling save for each related object / collection<br />

individually or by calling save on the object that owns the other objects. You can also call<br />

<strong>Doctrine</strong>_Connection::flush which saves all p<strong>en</strong>ding objects.<br />

// test.php<br />

Listing<br />

8-18<br />

// ...<br />

$user->Email['address'] = 'kosk<strong>en</strong>korva@drinkmore.info';<br />

$user->Phon<strong>en</strong>umbers[0]['phon<strong>en</strong>umber'] = '123123';<br />

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

In the above example calling $user->save() saves the email and phon<strong>en</strong>umber.<br />

Clearing Related Records<br />

You can clear a related records refer<strong>en</strong>ces from an object. This does not change the fact that<br />

these objects are related and won't change it in the database if you save. It just simply clears<br />

the refer<strong>en</strong>ce in PHP of one object to another.<br />

You can clear all refer<strong>en</strong>ces by doing the following:<br />

// test.php<br />

Listing<br />

8-19<br />

// ...<br />

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

Or you can clear a specific relationship:<br />

// test.php<br />

Listing<br />

8-20<br />

// ...<br />

$user->clearRelated('Email');<br />

This is useful if you were to do something like the following:<br />

// test.php<br />

Listing<br />

8-21<br />

// ...<br />

if ($user->Email->exists()) {<br />

// User has e-mail<br />

} else {<br />

// User does not have a e-mail<br />

}<br />

$user->clearRelated('Email');<br />

Because <strong>Doctrine</strong> will automatically create a new Email object if the user does not have one,<br />

we need to clear that refer<strong>en</strong>ce so that if we were to call $user->save() it wouldn't save a<br />

blank Email record for the User.<br />

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

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

Saved successfully!

Ooh no, something went wrong!