php-yii2-database-migration(Comprehensive guide)

Lucy KarimiLucy Karimi
3 min read

Table of contents

PHP Yii2 is a popular open-source web application framework that allows developers to build high-performance web applications with ease. One of the most important features of Yii2 is its built-in support for database migration, which simplifies the process of managing database schema changes across multiple environments. In this article, we will provide a comprehensive guide to PHP Yii2 Database Migration and show you how to use it to manage your database schema changes.

What is PHP Yii2 Database Migration?

PHP Yii2 Database Migration is a feature that allows developers to manage changes to their database schema over time. This means that as your application evolves, you can make changes to your database schema without having to manually update your database or lose your existing data. Database Migration in Yii2 is accomplished through a set of classes and commands that allow you to create, update, and roll back database schema changes.

How to Use PHP Yii2 Database Migration

To use PHP Yii2 Database Migration, you must first enable it in your Yii2 application. You can do this by adding the following code to your application configuration file:

return [
    // ...
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => '@app/migrations',
        ],
    ],
    // ...
];

This code configures the MigrateController class, which is responsible for managing your database migrations. The migrationPath parameter specifies the directory where your migration files will be stored.

Creating a Migration

To create a new migration, you can use the following command:

yii migrate/create create_users_table

This command creates a new migration file in your migration directory (@app/migrations by default) with a name that reflects the purpose of the migration (create_users_table in this example). The new migration file will contain an empty up() method and a down() method that can be used to undo the migration.

Updating a Migration

To update an existing migration, you can modify the up() and down() methods in the migration file. For example, if you wanted to add a new column to your users table, you could modify the up() method like this:

public function up()
{
    $this->addColumn('users', 'new_column', $this->string());
}

public function down()
{
    $this->dropColumn('users', 'new_column');
}

This code adds a new column named new_column to the users table. The down() method removes the column if the migration needs to be rolled back.

Running Migrations

To run your migrations, you can use the following command:

yii migrate

This command runs all of the pending migrations in your migration directory. Yii2 keeps track of which migrations have already been run, so you don't have to worry about running the same migration twice.

Rolling Back Migrations

If you need to roll back a migration, you can use the following command:

yii migrate/down

This command rolls back the last migration that was run. You can also specify a specific migration to roll back by providing its name as an argument to the command.

Conclusion

In conclusion, PHP Yii2 Database Migration is a powerful tool for managing your database schema changes over time. With Yii2's built-in support for migration, you can easily create, update, and roll back database schema changes without having to worry about losing your existing data. By following the steps outlined in this guide, you can start using PHP Yii2 Database Migration in your own Yii2 applications today.

0
Subscribe to my newsletter

Read articles from Lucy Karimi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lucy Karimi
Lucy Karimi