diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 08ea384d31..93f7bd2ff3 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -11,6 +11,7 @@ Yii Framework 2 Change Log - Bug #13306: Wildcard in `reloadableScripts` in `yii.js` allows 0 characters (arogachev) - Bug #13340: Fixed `yii\db\Connection::useMaster()` - Exception within callback completely disables slaves (Vovan-VE) - Bug #13343: Fixed `yii\i18n\Formatter::asTime()` to process time-only values without time zone conversion (bizley) +- Bug #13418: Fixed `QueryBuilder::batchInsert()` if $rows is `\Generator` (lav45) - Bug #13494: Fixed `yii\console\controllers\MessageConstroller::saveMessagesToDb()` to work on different DBMS correctly (silverfire) - Bug #13513: Fixed RBAC migration to work correctly on Oracle DBMS (silverfire) - Bug #13537: Fixed `yii\web\CacheSession::destroySession()` to work correctly when session is not written yet (silverfire, papalapa) diff --git a/framework/db/QueryBuilder.php b/framework/db/QueryBuilder.php index 959bb7c1f9..636a3c71f4 100644 --- a/framework/db/QueryBuilder.php +++ b/framework/db/QueryBuilder.php @@ -268,6 +268,9 @@ class QueryBuilder extends \yii\base\Object } $values[] = '(' . implode(', ', $vs) . ')'; } + if (empty($values)) { + return ''; + } foreach ($columns as $i => $name) { $columns[$i] = $schema->quoteColumnName($name); diff --git a/framework/db/oci/QueryBuilder.php b/framework/db/oci/QueryBuilder.php index 4bce7b1cac..829f87b4af 100644 --- a/framework/db/oci/QueryBuilder.php +++ b/framework/db/oci/QueryBuilder.php @@ -262,6 +262,9 @@ EOD; } $values[] = '(' . implode(', ', $vs) . ')'; } + if (empty($values)) { + return ''; + } foreach ($columns as $i => $name) { $columns[$i] = $schema->quoteColumnName($name); diff --git a/framework/db/pgsql/QueryBuilder.php b/framework/db/pgsql/QueryBuilder.php index 77b3ae91df..54fcfb7722 100644 --- a/framework/db/pgsql/QueryBuilder.php +++ b/framework/db/pgsql/QueryBuilder.php @@ -316,6 +316,9 @@ class QueryBuilder extends \yii\db\QueryBuilder } $values[] = '(' . implode(', ', $vs) . ')'; } + if (empty($values)) { + return ''; + } foreach ($columns as $i => $name) { $columns[$i] = $schema->quoteColumnName($name); diff --git a/framework/db/sqlite/QueryBuilder.php b/framework/db/sqlite/QueryBuilder.php index 41ec8fb711..4101f1a8f4 100644 --- a/framework/db/sqlite/QueryBuilder.php +++ b/framework/db/sqlite/QueryBuilder.php @@ -106,6 +106,9 @@ class QueryBuilder extends \yii\db\QueryBuilder } $values[] = implode(', ', $vs); } + if (empty($values)) { + return ''; + } foreach ($columns as $i => $name) { $columns[$i] = $schema->quoteColumnName($name); diff --git a/tests/framework/db/CommandTest.php b/tests/framework/db/CommandTest.php index b7ebad2747..5ce2ae246a 100644 --- a/tests/framework/db/CommandTest.php +++ b/tests/framework/db/CommandTest.php @@ -298,6 +298,15 @@ SQL; $this->assertEquals(0, $command->execute()); } + public function testBatchInsertWithYield() + { + if (version_compare(PHP_VERSION, '5.5', '<')) { + $this->markTestSkipped('The yield function is only supported with php 5.5 =< version'); + } else { + include __DIR__ . '/testBatchInsertWithYield.php'; + } + } + public function testInsert() { $db = $this->getConnection(); @@ -559,7 +568,6 @@ SQL; ], $records); } - public function testDropTable() { $db = $this->getConnection(); diff --git a/tests/framework/db/testBatchInsertWithYield.php b/tests/framework/db/testBatchInsertWithYield.php new file mode 100644 index 0000000000..6616d0fb03 --- /dev/null +++ b/tests/framework/db/testBatchInsertWithYield.php @@ -0,0 +1,18 @@ +getConnection()->createCommand(); +$command->batchInsert( + '{{customer}}', + ['email', 'name', 'address'], + $rows +); +$this->assertEquals(0, $command->execute());