Browse Source

Fix #17539: Fixed error when using `batch()` with `indexBy()` with MSSQL

tags/2.0.27
Alexander Kartavenko 5 years ago committed by Alexander Makarov
parent
commit
97499315a4
  1. 1
      framework/CHANGELOG.md
  2. 7
      framework/db/BatchQueryResult.php
  3. 24
      tests/framework/db/BatchQueryResultTest.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.27 under development 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 #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 #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) - Bug #16855: Ignore console commands that have no actions (alexeevdv)

7
framework/db/BatchQueryResult.php

@ -214,8 +214,11 @@ class BatchQueryResult extends BaseObject implements \Iterator
return $this->db->driverName; return $this->db->driverName;
} }
if (isset($this->_batch[0]->db->driverName)) { if (!empty($this->_batch)) {
return $this->_batch[0]->db->driverName; $key = array_keys($this->_batch)[0];
if (isset($this->_batch[$key]->db->driverName)) {
return $this->_batch[$key]->db->driverName;
}
} }
return null; return null;

24
tests/framework/db/BatchQueryResultTest.php

@ -36,13 +36,13 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
$query = new Query(); $query = new Query();
$query->from('customer')->orderBy('id'); $query->from('customer')->orderBy('id');
$batch = $query->batch(2, $db); $batch = $query->batch(2, $db);
$allRows = $this->getAllRowsFromBach($batch); $allRows = $this->getAllRowsFromBatch($batch);
$this->assertCount(3, $allRows); $this->assertCount(3, $allRows);
$this->assertEquals('user1', $allRows[0]['name']); $this->assertEquals('user1', $allRows[0]['name']);
$this->assertEquals('user2', $allRows[1]['name']); $this->assertEquals('user2', $allRows[1]['name']);
$this->assertEquals('user3', $allRows[2]['name']); $this->assertEquals('user3', $allRows[2]['name']);
// rewind // rewind
$allRows = $this->getAllRowsFromBach($batch); $allRows = $this->getAllRowsFromBatch($batch);
$this->assertCount(3, $allRows); $this->assertCount(3, $allRows);
// reset // reset
$batch->reset(); $batch->reset();
@ -60,7 +60,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
// query with index // query with index
$query = new Query(); $query = new Query();
$query->from('customer')->indexBy('name'); $query->from('customer')->indexBy('name');
$allRows = $this->getAllRowsFromBach($query->batch(2, $db)); $allRows = $this->getAllRowsFromBatch($query->batch(2, $db));
$this->assertCount(3, $allRows); $this->assertCount(3, $allRows);
$this->assertEquals('address1', $allRows['user1']['address']); $this->assertEquals('address1', $allRows['user1']['address']);
$this->assertEquals('address2', $allRows['user2']['address']); $this->assertEquals('address2', $allRows['user2']['address']);
@ -90,7 +90,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
$db = $this->getConnection(); $db = $this->getConnection();
$query = Customer::find()->orderBy('id'); $query = Customer::find()->orderBy('id');
$customers = $this->getAllRowsFromBach($query->batch(2, $db)); $customers = $this->getAllRowsFromBatch($query->batch(2, $db));
$this->assertCount(3, $customers); $this->assertCount(3, $customers);
$this->assertEquals('user1', $customers[0]->name); $this->assertEquals('user1', $customers[0]->name);
$this->assertEquals('user2', $customers[1]->name); $this->assertEquals('user2', $customers[1]->name);
@ -98,7 +98,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
// batch with eager loading // batch with eager loading
$query = Customer::find()->with('orders')->orderBy('id'); $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) { foreach ($customers as $customer) {
$this->assertTrue($customer->isRelationPopulated('orders')); $this->assertTrue($customer->isRelationPopulated('orders'));
} }
@ -111,14 +111,24 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
public function testBatchWithoutDbParameter() public function testBatchWithoutDbParameter()
{ {
$query = Customer::find()->orderBy('id')->limit(3); $query = Customer::find()->orderBy('id')->limit(3);
$customers = $this->getAllRowsFromBach($query->batch(2)); $customers = $this->getAllRowsFromBatch($query->batch(2));
$this->assertCount(3, $customers); $this->assertCount(3, $customers);
$this->assertEquals('user1', $customers[0]->name); $this->assertEquals('user1', $customers[0]->name);
$this->assertEquals('user2', $customers[1]->name); $this->assertEquals('user2', $customers[1]->name);
$this->assertEquals('user3', $customers[2]->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 = []; $allRows = [];
foreach ($batch as $rows) { foreach ($batch as $rows) {

Loading…
Cancel
Save