» » » How to add a new column with Yii2 migration on a specific table

How to add a new column with Yii2 migration on a specific table

posted in: Yii2.0 framework
3 min read

Today I want to add a new column into the existing table in Yii2. Instead of adding the new column via the model I prefer to add the new column via the Yii2 migration script. With a script, I can easily revert the scrip in the command line.

In order to work with the migration script, we will use the command line via the terminal. I use the bash terminal via VScode. You can use any terminal as you prefer.

What we have to do

  • create a migration script via command line
  • add the code for adding a new column into the table in the created script
  • run the migration script via command line
  • add the new column into Model and View

Yii.bat location

In my Yii application, the Yii command is located in root folder as below.

root folder/yii.bat

Useful links

  • The command line can be found here.
  • How to do the database migration can be found here.
  • How to add a new column into the database can be found here.

Create a migration script via command line

At the terminal, add the code below to create the migration script.

php yii migrate/create add_new_column_name_to_table_name

After running the command above, it will ask to confirm to create the new script. Simply type “yes” to confirm.

Create new migration 'D:\wamp64\www\my-application/console/migrations\m191109_081936_add_new_column_name_to_table_name.php'? (yes|no) [no]:

Once you type “yes” and hits Enter. You will see the successful message.

New migration created successfully.

Navigate to your migration folder, in my case, it locates at my-application/console/migration/. You will see the new migration file in there. In that file, you will see the code below.

<?php

use yii\db\Schema;
use jamband\schemadump\Migration;

class m191109_081936_add_new_column_name_to_table_name extends Migration
{
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
}

Add the code for adding a new column into the table in the created script

Add the new column as code below.

<?php

use yii\db\Schema;
use jamband\schemadump\Migration;

class m191109_081936_add_new_column_name_to_table_name extends Migration
{
    public function safeUp()
    {
        $this->addColumn('table_name', 'new_column_name', $this->integer()->defaultValue(1));
    }

    public function safeDown()
    {
        $this->dropColumn('table_name', 'new_column_name');
    }
}

Run the migration script via command line

Now back to the terminal and run the command below.

 php yii migrate

Once you run the command line above, you will see the message below. Just type “yes” in order to confirm.

$ php yii migrate
Yii Migration Tool (based on Yii v2.0.15.1)

Total 1 new migration to be applied:
        m191109_081936_add_new_column_name_to_table_name 

Apply the above migration? (yes|no) [no]:

If the command runs successful, you will see the message below.

$ php yii migrate
Yii Migration Tool (based on Yii v2.0.15.1)

Total 1 new migration to be applied:
        m191109_081936_add_new_column_name_to_table_name 

Apply the above migration? (yes|no) [no]:yes
*** applying m191109_081936_add_new_column_name_to_table_name 
    > add column new_column_name integer DEFAULT 1 to table table_name ... done (time: 1.411s)
*** applied m191109_081936_add_new_column_name_to_table_name (time: 1.635s)


1 migration was applied.

Migrated up successfully.

Now when you check your table structure, you will see the new column added to the table you want.

Add the new column into your Model and View

Now the table structure is changed. Next, we need to add the new column into the form and view files. In my case, the new column will add via the radioList.

Model class – you have to add the code below to the model class that you just add

In my model class, I add the new column to the rules function.

[['new_column_name'], 'integer'],

Then I add the label to attributeLabels function. Because my application has two languages, I need to add the label here so I can do the text translation for the second language.

'new_column_name' => Yii::t('frontend', 'New Column Label')
_form.php in views folder

Add the new column to form. This form is used in both adding and updating form.

        <?php
        // set default value of radioList
        $model->isNewRecord == 1 ? $model->new_column_name = 0 : $model->new_column_name;
        ?>
        <?= $form->field($model, 'new_column_name')->radioList(
            [
                '1' => 'Yes',
                '0' => 'No'
            ]
        ); ?>
view.php in views folder
                [ // 'new_column_name',
                    'attribute' => 'new_column_name',
                    'value' => function ($data) {
                        if ($data['new_column_name'] === 1) {
                            return 'Yes';
                        }
                        return 'No';
                    },
                ],
index.php in views folder
[ // 'new_column_name',
                    'attribute' => 'new_column_name',
                    'value' => function ($data) {
                        if ($data['new_column_name'] === 1) {
                            return '<span class="label label-primary">' . 'Yes' . '</span>';
                        }
                        return '<span class="label label-success">' . 'No' . '</span>';
                    },
                    'format' => 'raw',
                ],

Note that, you don’t need to add the format option if you don’t add any HTLM or CSS in return.

Now you can test it by adding a new record and updating the existing one. It should have no error and work as you expect.