

./yii migrate/create insert_to_plan_attended_user_relation

<pre class="wp-block-syntaxhighlighter-code">
<?php
use yii\db\Migration;
/**
* Class m190411_092923_insert_to_plan_attended_user_relation
*/
class m190411_092923_insert_to_plan_attended_user_relation extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m190411_092923_insert_to_plan_attended_user_relation cannot be reverted.\n";
return false;
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m190411_092923_insert_to_plan_attended_user_relation cannot be reverted.\n";
return false;
}
*/
}
</pre>
<pre class="wp-block-syntaxhighlighter-code">
<?php
use yii\db\Connection;
use yii\db\Exception;
use yii\db\Migration;
use yii\db\Query;
/**
* Class m190411_092923_insert_to_plan_attended_user_relation
*/
class m190411_092923_insert_to_plan_attended_user_relation extends Migration
{
/**
* {@inheritdoc}
* @throws Exception if connection fails
*/
public function safeUp()
{
// 创建一个单独的非缓存链接到数据库
$unbufferedDb = new Connection([
'dsn' => Yii::$app->db->dsn,
'username' => Yii::$app->db->username,
'password' => Yii::$app->db->password,
'tablePrefix' => Yii::$app->db->tablePrefix,
'charset' => Yii::$app->db->charset,
]);
$unbufferedDb->open();
// 设置 PDO 连接属性 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 为 false
$unbufferedDb->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$query = (new Query())
->from('{{%plan}}')
->orderBy(['id' => SORT_ASC]);
/* 创建 MySQL 模型(选题与参与用户的关联) */
$table = '{{%plan_attended_user_relation}}';
$columns = ['group_id', 'config_column_id', 'plan_id', 'relation_user_id', 'role', 'status', 'is_not_isolated', 'is_deleted', 'created_at', 'updated_at', 'deleted_at'];
foreach ($query->batch(10, $unbufferedDb) as $plans) {
// $plans 是一个包含100条或小于100条用户表数据的数组
$rows = [];
foreach ($plans as $plan) {
$rows[] = [
$plan['group_id'],
$plan['config_column_id'],
$plan['id'],
$plan['exec_user_id'],
1,
1,
$plan['is_not_isolated'],
$plan['is_deleted'],
$plan['created_at'],
0,
$plan['deleted_at'],
];
}
$this->batchInsert($table, $columns, $rows);
}
$unbufferedDb->close();
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m190411_092923_insert_to_plan_attended_user_relation cannot be reverted.\n";
return false;
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m190411_092923_insert_to_plan_attended_user_relation cannot be reverted.\n";
return false;
}
*/
}
</pre>

PS E:\wwwroot\pcs-api> ./yii migrate
Yii Migration Tool (based on Yii v2.0.15.1)
Total 1 new migration to be applied:
m190411_092923_insert_to_plan_attended_user_relation
Apply the above migration? (yes|no) [no]:yes
*** applying m190411_092923_insert_to_plan_attended_user_relation
> insert into {{%plan_attended_user_relation}} ... done (time: 0.013s)
> insert into {{%plan_attended_user_relation}} ... done (time: 0.003s)
> insert into {{%plan_attended_user_relation}} ... done (time: 0.002s)
*** applied m190411_092923_insert_to_plan_attended_user_relation (time: 0.050s)
1 migration was applied.
Migrated up successfully.

PHP / Laravel / Yii2 老项目维护与长期技术支持
如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。
适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护
可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查
如需咨询,请联系我,并注明:PHP 维护咨询。
联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

发表回复