yii 2 create migration with fields

To create a migration with fields in Yii 2, you can follow these steps:

  1. Open your command line interface and navigate to the root directory of your Yii 2 application.

  2. Run the following command to generate a new migration file:

php yii migrate/create <migration_name>

Replace <migration_name> with the desired name for your migration.

  1. Open the generated migration file located in the migrations directory.

  2. Inside the up() method, use the createTable() method to define the table and its fields. For example:

$this->createTable('table_name', [
    'id' => $this->primaryKey(),
    'field1' => $this->string(),
    'field2' => $this->integer(),
    // Add more fields as needed

Replace 'table_name' with the actual name of your table, and add more fields as needed.

  1. Inside the down() method, use the dropTable() method to drop the table. For example:

Replace 'table_name' with the actual name of your table.

  1. Save the migration file.

  2. Run the migration by executing the following command in your command line interface:

php yii migrate

This will apply the migration and create the corresponding table in your database.

That's it! You have successfully created a migration with fields in Yii 2.