Search code examples
phpdatabaseyiiyii2migration

Yii2 Migration move data to other table


I have table:

 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;

and I have created table

        $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');

I want to move some data(date_used & used_by_user_id) from table ticket to other table used with migration yii2 in the query. But I don't know how to do it without ActiveRecord & array.


Solution

  • $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
    ");
    

    Only sql. Yii-migration not supported select in the insert into