diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 88dae9b..7d13da1 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.27 under development ------------------------ +- Bug #17539: Fixed error when using `batch()` with `indexBy()` with MSSQL (alexkart) - Bug #17549: Fix `yii\db\ExpressionInterface` not supported in `yii\db\conditions\SimpleConditionBuilder` (razvanphp) - Bug #17434: Fix regular expression illegal character; Repeated fix for Internet Explorer 11 AJAX redirect bug in case of 301 and 302 response codes (`XMLHttpRequest: Network Error 0x800c0008`) (kamarton) - Bug #16855: Ignore console commands that have no actions (alexeevdv) diff --git a/framework/db/BatchQueryResult.php b/framework/db/BatchQueryResult.php index 4dd9723..68fd03f 100644 --- a/framework/db/BatchQueryResult.php +++ b/framework/db/BatchQueryResult.php @@ -214,8 +214,11 @@ class BatchQueryResult extends BaseObject implements \Iterator return $this->db->driverName; } - if (isset($this->_batch[0]->db->driverName)) { - return $this->_batch[0]->db->driverName; + if (!empty($this->_batch)) { + $key = array_keys($this->_batch)[0]; + if (isset($this->_batch[$key]->db->driverName)) { + return $this->_batch[$key]->db->driverName; + } } return null; diff --git a/tests/framework/db/BatchQueryResultTest.php b/tests/framework/db/BatchQueryResultTest.php index 24d9fde..4fbae97 100644 --- a/tests/framework/db/BatchQueryResultTest.php +++ b/tests/framework/db/BatchQueryResultTest.php @@ -36,13 +36,13 @@ abstract class BatchQueryResultTest extends DatabaseTestCase $query = new Query(); $query->from('customer')->orderBy('id'); $batch = $query->batch(2, $db); - $allRows = $this->getAllRowsFromBach($batch); + $allRows = $this->getAllRowsFromBatch($batch); $this->assertCount(3, $allRows); $this->assertEquals('user1', $allRows[0]['name']); $this->assertEquals('user2', $allRows[1]['name']); $this->assertEquals('user3', $allRows[2]['name']); // rewind - $allRows = $this->getAllRowsFromBach($batch); + $allRows = $this->getAllRowsFromBatch($batch); $this->assertCount(3, $allRows); // reset $batch->reset(); @@ -60,7 +60,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase // query with index $query = new Query(); $query->from('customer')->indexBy('name'); - $allRows = $this->getAllRowsFromBach($query->batch(2, $db)); + $allRows = $this->getAllRowsFromBatch($query->batch(2, $db)); $this->assertCount(3, $allRows); $this->assertEquals('address1', $allRows['user1']['address']); $this->assertEquals('address2', $allRows['user2']['address']); @@ -90,7 +90,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase $db = $this->getConnection(); $query = Customer::find()->orderBy('id'); - $customers = $this->getAllRowsFromBach($query->batch(2, $db)); + $customers = $this->getAllRowsFromBatch($query->batch(2, $db)); $this->assertCount(3, $customers); $this->assertEquals('user1', $customers[0]->name); $this->assertEquals('user2', $customers[1]->name); @@ -98,7 +98,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase // batch with eager loading $query = Customer::find()->with('orders')->orderBy('id'); - $customers = $this->getAllRowsFromBach($query->batch(2, $db)); + $customers = $this->getAllRowsFromBatch($query->batch(2, $db)); foreach ($customers as $customer) { $this->assertTrue($customer->isRelationPopulated('orders')); } @@ -111,14 +111,24 @@ abstract class BatchQueryResultTest extends DatabaseTestCase public function testBatchWithoutDbParameter() { $query = Customer::find()->orderBy('id')->limit(3); - $customers = $this->getAllRowsFromBach($query->batch(2)); + $customers = $this->getAllRowsFromBatch($query->batch(2)); $this->assertCount(3, $customers); $this->assertEquals('user1', $customers[0]->name); $this->assertEquals('user2', $customers[1]->name); $this->assertEquals('user3', $customers[2]->name); } - protected function getAllRowsFromBach(BatchQueryResult $batch) + public function testBatchWithIndexBy() + { + $query = Customer::find()->orderBy('id')->limit(3)->indexBy('id'); + $customers = $this->getAllRowsFromBatch($query->batch(2)); + $this->assertCount(3, $customers); + $this->assertEquals('user1', $customers[0]->name); + $this->assertEquals('user2', $customers[1]->name); + $this->assertEquals('user3', $customers[2]->name); + } + + protected function getAllRowsFromBatch(BatchQueryResult $batch) { $allRows = []; foreach ($batch as $rows) {