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 179<br />

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

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

Function Callbacks for Columns<br />

<strong>Doctrine</strong>_Record offers a way for attaching callback calls for column values. For example if<br />

you want to trim certain column, you can simply use:<br />

// test.php<br />

Listing<br />

10-72<br />

// ...<br />

$record->call('trim', 'username');<br />

Collection<br />

<strong>Doctrine</strong>_Collection is a collection of records (see <strong>Doctrine</strong>_Record). As with records the<br />

collections can be deleted and saved using <strong>Doctrine</strong>_Collection::delete() and<br />

<strong>Doctrine</strong>_Collection::save() accordingly.<br />

Wh<strong>en</strong> fetching data from database with either DQL API (see <strong>Doctrine</strong>_Query) or rawSql<br />

API (see <strong>Doctrine</strong>_RawSql) the methods return an instance of <strong>Doctrine</strong>_Collection by<br />

default.<br />

The following example shows how to initialize a new collection:<br />

// test.php<br />

Listing<br />

10-73<br />

// ...<br />

$users = new <strong>Doctrine</strong>_Collection('User');<br />

Now add some new data to the collection:<br />

// test.php<br />

Listing<br />

10-74<br />

// ...<br />

$users[0]->username = 'Arnold';<br />

$users[1]->username = 'Somebody';<br />

Now just like we can delete a collection we can save it:<br />

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

Listing<br />

10-75<br />

Accessing Elem<strong>en</strong>ts<br />

You can access the elem<strong>en</strong>ts of <strong>Doctrine</strong>_Collection with set() and get() methods or<br />

with ArrayAccess interface.<br />

// test.php<br />

Listing<br />

10-76<br />

// ...<br />

$userTable = <strong>Doctrine</strong>_Core::getTable('User');<br />

$users = $userTable->findAll();<br />

Accessing elem<strong>en</strong>ts with ArrayAccess interface<br />

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

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

Saved successfully!

Ooh no, something went wrong!