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 24: Migrations 328<br />

Omitting the version number argum<strong>en</strong>t for the migrate() method means that internally<br />

<strong>Doctrine</strong> will try and migrate to the latest class version number that it could find in the<br />

directory you passed.<br />

Transactions in Migrations<br />

Internally <strong>Doctrine</strong> does not wrap migration versions in transactions. It is up to you as the<br />

developer to handle exceptions and transactions in your migration classes. Remember<br />

though, that very few databases support transactional DDL. So on most databases, ev<strong>en</strong> if<br />

you wrap the migrations in a transaction, any DDL statem<strong>en</strong>ts like create, alter, drop,<br />

r<strong>en</strong>ame, etc., will take effect anyway.<br />

Implem<strong>en</strong>t<br />

Now that we know how to perform migrations lets implem<strong>en</strong>t a little script in our <strong>Doctrine</strong><br />

test <strong>en</strong>vironm<strong>en</strong>t named migrate.php.<br />

First we need to create a place for our migration classes to be stored so lets create a<br />

directory named migrations:<br />

Listing<br />

24-6<br />

$ mkdir migrations<br />

Now create the migrate.php script in your favorite editor and place the following code<br />

inside:<br />

Listing<br />

24-7<br />

// migrate.php<br />

require_once('bootstrap.php');<br />

$migration = new <strong>Doctrine</strong>_Migration('migrations');<br />

$migration->migrate();<br />

Writing Migration Classes<br />

Migration classes consist of a simple class that ext<strong>en</strong>ds from <strong>Doctrine</strong>_Migration. You can<br />

define a up() and down() method that is meant for doing and undoing changes to a database<br />

for that migration version.<br />

The name of the class can be whatever you want, but the name of the file which contains<br />

the class must have a prefix containing a number that is used for loading the migrations in<br />

the correct order.<br />

Below are a few examples of some migration classes you can use to build your database<br />

starting from version 1.<br />

For the first version lets create a new table named migration_test:<br />

Listing<br />

24-8<br />

// migrations/1_add_table.php<br />

class AddTable ext<strong>en</strong>ds <strong>Doctrine</strong>_Migration_Base<br />

{<br />

public function up()<br />

{<br />

$this->createTable('migration_test', array('field1' =><br />

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

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

Saved successfully!

Ooh no, something went wrong!