Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.0.2 under development

- New #332: Add `MigrationBuilder::insertBatch()` method, deprecate `batchInsert()` (@vjik)
- Enh #317: Explicitly import classes, functions, and constants in "use" section (@mspirkov)
- Enh #318: Remove unnecessary files from Composer package (@mspirkov)
- Enh #319: Remove confirmation prompt from `migrate:create` command as creating a migration is non-destructive (@samdark)
Expand Down
26 changes: 25 additions & 1 deletion src/MigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,39 @@ public function insert(string $table, array $columns): void
* The method will properly escape the column names and bind the values to be inserted.
*
* @param string $table The table that new rows will be inserted into.
* @param iterable $rows The rows to be batch inserted into the table.
* @param string[] $columns The column names.
* @param iterable $rows The rows to be batch inserted into the table
*
* @psalm-param iterable<iterable<array-key, mixed>> $rows
*
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
*/
public function insertBatch(string $table, iterable $rows, array $columns = []): void
{
$time = $this->beginCommand("Insert into $table");
$this->db->createCommand()->insertBatch($table, $rows, $columns)->execute();
$this->endCommand($time);
}

/**
* Creates and executes a batch INSERT SQL statement.
*
* The method will properly escape the column names and bind the values to be inserted.
*
* @param string $table The table that new rows will be inserted into.
* @param string[] $columns The column names.
* @param iterable $rows The rows to be batch inserted into the table.
*
* @psalm-param iterable<iterable<array-key, mixed>> $rows
*
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
*
* @deprecated Use {@see insertBatch()} instead.
*/
public function batchInsert(string $table, array $columns, iterable $rows): void
{
$time = $this->beginCommand("Insert into $table");
Expand Down
28 changes: 28 additions & 0 deletions tests/Common/AbstractMigrationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ public function testInsert(): void
$this->builder->dropTable('test');
}

public function testInsertBatch(): void
{
$this->builder->createTable('test', ['id' => ColumnBuilder::integer()]);
$this->builder->insertBatch('test', [['id' => 1], ['id' => 2]]);

$this->assertEquals(
'2',
$this->db->createCommand('SELECT count(*) FROM {{test}} WHERE [[id]] IN (1, 2)')->queryScalar(),
);
$this->assertInformerOutputContains(' > Insert into test ... Done in ');

$this->builder->dropTable('test');
}

public function testInsertBatchWithColumns(): void
{
$this->builder->createTable('test', ['id' => ColumnBuilder::integer()]);
$this->builder->insertBatch('test', [[1], [2]], ['id']);

$this->assertEquals(
'2',
$this->db->createCommand('SELECT count(*) FROM {{test}} WHERE [[id]] IN (1, 2)')->queryScalar(),
);
$this->assertInformerOutputContains(' > Insert into test ... Done in ');

$this->builder->dropTable('test');
}

public function testBatchInsert(): void
{
$this->builder->createTable('test', ['id' => ColumnBuilder::integer()]);
Expand Down
Loading