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 10: Compon<strong>en</strong>t Overview 178<br />

Saving a Blank Record<br />

By default <strong>Doctrine</strong> doesn't execute wh<strong>en</strong> save() is being called on an unmodified record.<br />

There might be situations where you want to force-insert the record ev<strong>en</strong> if it has not be<strong>en</strong><br />

modified. This can be achieved by assigning the state of the record to<br />

<strong>Doctrine</strong>_Record::STATE_TDIRTY.<br />

Listing<br />

10-68<br />

// test.php<br />

// ...<br />

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

$user->state('TDIRTY');<br />

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

Wh<strong>en</strong> setting the state you can optionally pass a string for the state and it will be converted<br />

to the appropriate state constant. In the example above, TDIRTY is actually converted to<br />

<strong>Doctrine</strong>_Record::STATE_TDIRTY.<br />

Mapping Custom Values<br />

There might be situations where you want to map custom values to records. For example<br />

values that dep<strong>en</strong>d on some outer sources and you only want these values to be available at<br />

runtime not persisting those values into database. This can be achieved as follows:<br />

Listing<br />

10-69<br />

// test.php<br />

// ...<br />

$user->mapValue('isRegistered', true);<br />

$user->isRegistered; // true<br />

Serializing<br />

Sometimes you may want to serialize your record objects (possibly for caching purposes):<br />

Listing<br />

10-70<br />

// test.php<br />

// ...<br />

$string = serialize($user);<br />

$user = unserialize($string);<br />

Checking Exist<strong>en</strong>ce<br />

Very commonly you'll need to know if giv<strong>en</strong> record exists in the database. You can use the<br />

exists() method for checking if giv<strong>en</strong> record has a database row equival<strong>en</strong>t:<br />

Listing<br />

10-71<br />

// test.php<br />

// ...<br />

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

echo $record->exists() ? 'Exists':'Does Not Exist'; // Does Not Exist<br />

$record->username = 'someone';<br />

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

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

Saved successfully!

Ooh no, something went wrong!