D
D
des1roer2016-03-22 08:13:55
Yii
des1roer, 2016-03-22 08:13:55

Yii 2 migrations inserting multiple rows?

how can i insert multiple rows and delete also a few

public function safeUp()
    {
        $this->insert('country', [
            'name' => 'Австралия',
            'name' => 'Австрия',
            'name' => 'Азербайджан',
            'name' => 'Албания',
        ]);
    }

    public function safeDown()
    {
        $this->delete('country', [
            'name' => 'Австралия',
            'name' => 'Австрия',
            'name' => 'Азербайджан',
            'name' => 'Албания',
        ]);
    }

so only one is inserted

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
des1roer, 2016-03-22
@des1roer

<?php

use yii\db\Migration;

class m160322_045630_add_catalog_data extends Migration {

    public function safeUp()
    {
        Yii::$app->db->createCommand()->batchInsert('country', ['name'], [
            ['Австралия'],
            ['Австрия'],
            ['Азербайджан'],
            ['Албания'],
        ])->execute();
 
    }

    public function safeDown()
    {
        Yii::$app->db->createCommand()->delete('country', ['in', 'name', ['Австралия', 'Австрия', 'Азербайджан', 'Албания']]
        )->execute();

    }

A
Alexander, 2016-03-22
@p0vidl0

batchInsert()

public function up()
    {
        $this->batchInsert('country', ['name'], [
          ['Австралия'],
          [ 'Австрия'],
          ['Азербайджан'],
          [ 'Албания'],
        ]);
    }

public function down()
    {
        $this->delete('country', ['in', 'name', [
           'Австралия',
            'Австрия',
           'Азербайджан',
           'Албания',
        ]]);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question