【问题标题】:Yii2 Migration move data to other tableYii2 Migration 将数据移动到其他表
【发布时间】:2018-10-02 10:18:24
【问题描述】:

我有桌子:

 CREATE TABLE `ticket` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price` int(11) DEFAULT NULL,
  `status` NOT NULL DEFAULT,
  `date_created` datetime NOT NULL,
  `date_used` datetime DEFAULT NULL,
  `used_by_user_id` int(11) DEFAULT NULL,) 
 ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我已经创建了表格

        $this->createTable('used', [
        'id'              => $this->primaryKey(),
        'ticket_id'        => $this->integer()->notNull(),
        'date_used'        => $this->integer()->notNull(),
        'used_by_user_id' => $this->integer()->notNull(),
], 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB');

我想将一些数据(date_used & used_by_user_id)从表 ticket 移动到查询中与迁移 yii2 一起使用的其他表。但是如果没有ActiveRecord & array,我不知道该怎么做。

【问题讨论】:

    标签: php database yii yii2 migration


    【解决方案1】:
    $this->execute("
    INSERT INTO used (ticket_id, date_used, used_by_user_id) 
    SELECT id, date_used, used_by_user_id 
    FROM ticket 
    WHERE 
    used_by_user_id IS NOT NULL AND date_used IS NOT NULL
    ");
    

    只有 sql。 insert into 中不支持 Yii-migration select

    【讨论】:

    • 转储代码不是建议答案时推荐的方式,您应该添加一些关于您的代码、它在做什么以及为什么遇到相同问题的人应该使用它的描述。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2015-07-22
    • 2017-12-21
    相关资源
    最近更新 更多