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

Listing<br />

24-34<br />

Listing<br />

24-35<br />

Listing<br />

24-36<br />

Listing<br />

24-37<br />

Diff Tool<br />

Sometimes you may want to alter your models and be able to automate the migration process<br />

for your changes. In the past you would have to write the migration classes <strong>manual</strong>ly for your<br />

changes. Now with the diff tool you can make your changes th<strong>en</strong> g<strong>en</strong>erate the migration<br />

classes for the changes.<br />

The diff tool is simple to use. It accepts a "from" and a "to" and they can be one of the<br />

following:<br />

• Path to yaml schema files<br />

• Name of an existing database connection<br />

• Path to an existing set of models<br />

A simple example would be to create two YAML schema files, one named schema1.yml and<br />

another named schema2.yml.<br />

The schema1.yml contains a simple User model:<br />

---<br />

# schema1.yml<br />

User:<br />

columns:<br />

username: string(255)<br />

password: string(255)<br />

Now imagine we modify the above schema and want to add a email_address column:<br />

---<br />

# schema1.yml<br />

User:<br />

columns:<br />

username: string(255)<br />

password: string(255)<br />

email_address: string(255)<br />

Now we can easily g<strong>en</strong>erate a migration class which will add the new column to our database:<br />

<strong>Doctrine</strong>_Core::g<strong>en</strong>erateMigrationsFromDiff('/path/to/migration/classes',<br />

'/path/to/schema1.yml', '/path/to/schema2.yml');<br />

This will produce a file at the path /path/to/migration/classes/<br />

1236199329_version1.php<br />

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

{<br />

public function up()<br />

{<br />

$this->addColumn('user', 'email_address', 'string', '255', array<br />

());<br />

}<br />

}<br />

public function down()<br />

{<br />

$this->removeColumn('user', 'email_address');<br />

}<br />

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

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

Saved successfully!

Ooh no, something went wrong!