Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5d8b9a4850
file 68 lines (38 sloc) 3.406 kb

Setting up migrations in your project

If you want to use migrations in your symfony projects you have three options. You can either use migrations right from the start of your project, you can create migrations from existing model files or you can create migrations from an existing database.

Starting on a new project

When starting on a fresh project you first need to create the database you want to use for your project. This can easily be done by letting Doctrine build it.

$ php symfony doctrine:build-db

Now that the database is ready you need to create a database schema in your config/doctrine/schema.yml file. You could for example create a small table to save blog posts in your database.

    Timestampable: ~
    title: string(255)
    body: clob

After you define the tables in your schema.yml file you need to generate the first version of your model files - Doctrine will then use these to generate the first migration class for you. Since symfony 1.3 it has become possible to use the doctrine:build task to do generate the model.

$ php symfony doctrine:build --all-classes

Now you have your model files but no migration class and no tables in your database. Use the following command to generate the baseline migration class from the model.

$ php symfony doctrine:generate-migrations-model

This command generates a migration class which defines version 1 of your database, but it does not apply those changes to the database itself. To finally migrate the changes into your database you have to use the following task.

$ php symfony doctrine:migrate

Your database is now up to date and ready to be used.

Starting with an existing database

If you already have an existing database you can still use migrations in your project. Use the doctrine:generate-migrations-db task and Doctrine will inspect your database and create a baseline migration based on the current state.

$ php symfony doctrine:generate-migrations-db

After generating the migrations from your database you now need to drop and rebuild it so that Doctrine can start to use migration versions in it.

Note: if you have data in your database you will need to reload it after you set up migrations!

$ php symfony doctrine:drop-db
$ php symfony doctrine:build-db

Now that your database is set up again you initialize the migrations by using the doctrine:build task.

$ php symfony doctrine:build --all-classes --and-migrate

Starting with an existing model

If you want to start using migrations and you already have both an existing database and generated model files you are free to use either the doctrine:generate-migrations-db or doctrine:generate-migrations-model tasks. Note: it is important to use only one and not both!

Personally I prefer (for no particular reason) to use the doctrine:-generate-migrations-model task. So, to start using migrations looks very similar to the way which is already described in the last section.

Use the doctrine:generate-migrations-model task to generate your initial migrations.

$ php symfony doctrine:generate-migrations-model

Drop and re-build the database.

$ php symfony doctrine:drop-db
$ php symfony doctrine:build-db

And migrate your database so that all your tables are re-created again.

$ php symfony doctrine:migrate
Something went wrong with that request. Please try again.