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 20: Database Abstraction Layer 298<br />

$conn->export->alterTable('ev<strong>en</strong>ts', $alter);<br />

Notice how we r<strong>en</strong>amed the table to ev<strong>en</strong>t, lets r<strong>en</strong>ame it back to ev<strong>en</strong>ts. We only<br />

r<strong>en</strong>amed it to demonstrate the functionality and we will need the table to be named<br />

ev<strong>en</strong>ts for the next examples.<br />

Listing<br />

20-12<br />

// test.php<br />

// ...<br />

$alter = array(<br />

'name' => 'ev<strong>en</strong>ts'<br />

);<br />

$conn->export->alterTable('ev<strong>en</strong>t', $alter);<br />

Creating Indexes<br />

To create an index, the method createIndex() is used, which has similar signature as<br />

createConstraint(), so it takes table name, index name and a definition array. The<br />

definition array has one key named fields with a value which is another associative array<br />

containing fields that will be a part of the index. The fields are defined as arrays with possible<br />

keys: sorting, with values asc<strong>en</strong>ding and desc<strong>en</strong>ding l<strong>en</strong>gth, integer value<br />

Not all RDBMS will support index sorting or l<strong>en</strong>gth, in these cases the drivers will ignore<br />

them. In the test ev<strong>en</strong>ts database, we can assume that our application will show ev<strong>en</strong>ts<br />

occuring in a specific timeframe, so the selects will use the datetime field in WHERE<br />

conditions. It will help if there is an index on this field.<br />

Listing<br />

20-13<br />

// test.php<br />

// ...<br />

$definition = array(<br />

'fields' => array(<br />

'datetime' => array()<br />

)<br />

);<br />

$conn->export->createIndex('ev<strong>en</strong>ts', 'datetime', $definition);<br />

Deleting database elem<strong>en</strong>ts<br />

For every create*() method as shown above, there is a corresponding drop*() method to<br />

delete a database, a table, field, index or constraint. The drop*() methods do not check if<br />

the item to be deleted exists, so it's developer's responsibility to check for exceptions using a<br />

try catch block:<br />

Listing<br />

20-14<br />

// test.php<br />

// ...<br />

try {<br />

$conn->export->dropSequ<strong>en</strong>ce('nonexisting');<br />

} catch(<strong>Doctrine</strong>_Exception $e) {<br />

}<br />

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

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

Saved successfully!

Ooh no, something went wrong!