In Yii 2.0, migrate a column of data in the A table in the MySQL database to a column of data in the B table, based on the implementation of a SQL
1. Reference URL:https://www.shuijingwanwq.com/2019/08/15/3427/
UPDATE `cpa_channel_app_source`, `cpa_weibo_weibo_connect_web_app_user` SET `cpa_channel_app_source`.`permission` = `cpa_weibo_weibo_connect_web_app_user`.`permission` WHERE `cpa_channel_app_source`.`id` = `cpa_weibo_weibo_connect_web_app_user`.`channel_app_source_id`;
2. Update table: field in channel_app_source: The value of Permission is: 3, as shown in Figure 1
3. Based on the database migration command, generate the implementation of a SQL as shown above, edit \console\migrations\m190815_020034_permission.php
update('{{%channel_app_source}}, {{%weibo_weibo_connect_web_app_user}}', ['{{%channel_app_source}}.permission' => new Expression('{{%weibo_weibo_connect_web_app_user}}.permission')], ['{{%channel_app_source}}.id' => new Expression('{{%weibo_weibo_connect_web_app_user}}.channel_app_source_id')]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m190815_020034_permission cannot be reverted.\n";
return false;
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m190815_020034_permission cannot be reverted.\n";
return false;
}
*/
}
4. Execute the database migration command, as shown in Figure 2
PS E:\wwwroot\channel-pub-api> ./yii migrate
Yii Migration Tool (based on Yii v2.0.15.1)
Total 1 new migration to be applied:
m190815_020034_permission
Apply the above migration? (yes|no) [no]:yes
*** applying m190815_020034_permission
> update {{%channel_app_source}}, {{%weibo_weibo_connect_web_app_user}} ... done (time: 0.092s)
*** applied m190815_020034_permission (time: 0.190s)
1 migration was applied.
Migrated up successfully.
5. Check the table: the field in channel_app_source: the value of Permission is: 2, which is in line with expectations, as shown in Figure 3


