yii 2 create migration with fields
To create a migration with fields in Yii 2, you can follow these steps:
Open your command line interface and navigate to the root directory of your Yii 2 application.
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.
Open the generated migration file located in the
migrations
directory.Inside the
up()
method, use thecreateTable()
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.
- Inside the
down()
method, use thedropTable()
method to drop the table. For example:
$this->dropTable('table_name');
Replace 'table_name'
with the actual name of your table.
Save the migration file.
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.