Home  >  Article  >  Backend Development  >  How to use database migration (Migration) function in Yii framework

How to use database migration (Migration) function in Yii framework

WBOY
WBOYOriginal
2023-07-28 21:57:201512browse

How to use the database migration (Migration) function in the Yii framework

Introduction:
Database migration is a very important tool when developing and maintaining a Web application. Database migration can help us manage changes to the database schema, allowing us to easily create, modify, and delete tables, as well as add, delete, and modify columns. This article will introduce how to use the database migration (Migration) function in the Yii framework.

1. Configure database information
Before using database migration, we need to configure the database information first. Open the db.php file in the config directory in the Yii framework, and modify the following relevant codes according to your own database configuration:

return [
    // ...
    'components' => [
        // ...
        'db' => [
            'class' => 'yiidbConnection',
            'dsn' => 'mysql:host=localhost;dbname=mydatabase',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
];

2. Create a migration file
In the Yii framework, each database migration can be an independent PHP class. We can create new migration files through Yii's command line tool. Open a command line terminal, enter the root directory of the Yii project, and execute the following command:

./yii migrate/create create_table_user

The above command will create a migration file named m210101_000000_create_table_user. This file will be located in the consolemigrations directory.

3. Write migration code
Open the migration file just created, we can see that it contains two functions: up() and down().

use yiidbMigration;

class m210101_000000_create_table_user extends Migration
{
    public function up()
    {
        $this->createTable('user', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull(),
            'email' => $this->string()->notNull(),
            'password' => $this->string()->notNull(),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ]);
    }

    public function down()
    {
        $this->dropTable('user');
    }
}

In the up() function, we use the createTable() method to create a table named user and define Its various fields and data types.

In the down() function, we use the dropTable() method to delete the previously created user table.

4. Execute the migration operation
After writing the migration code, we need to perform the migration operation to apply our database schema changes to the actual database. Execute the following command in the command line terminal:

./yii migrate

The above command will execute all unexecuted migration files and apply the database schema changes to the actual database.

In addition to executing all outstanding migrations, we can also execute the following command to perform some other specific migration operations:

  • ./yii migrate/new: Display all unexecuted migration files;
  • ./yii migrate/redo: Redo the last migration operation;
  • ./yii migrate /down: Roll back the last migration operation;
  • ./yii migrate/create: Create a new migration file.

Summary:
This article introduces the basic steps for using the database migration function in the Yii framework. We first configured the database information, then created a migration file and wrote the corresponding migration code. Finally, we performed the migration using Yii's command line tools. By using the database migration feature, we can easily manage and track database schema changes. This is a very convenient and practical tool for the development and maintenance of our web applications.

The above is the detailed content of How to use database migration (Migration) function in Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn