diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8621d032b7..fac12b0841 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 Change Log 2.0.25 under development ------------------------ -- no changes in this release. +- Bug #17486: Fixed error when using `batch()` without `$db` parameter with MSSQL (alexkart) 2.0.24 July 30, 2019 diff --git a/framework/db/BatchQueryResult.php b/framework/db/BatchQueryResult.php index fd7391c691..4dd972308a 100644 --- a/framework/db/BatchQueryResult.php +++ b/framework/db/BatchQueryResult.php @@ -151,8 +151,8 @@ class BatchQueryResult extends BaseObject implements \Iterator /** * Reads and collects rows for batch - * @since 2.0.23 * @return array + * @since 2.0.23 */ protected function getRows() { @@ -165,7 +165,7 @@ class BatchQueryResult extends BaseObject implements \Iterator } } catch (\PDOException $e) { $errorCode = isset($e->errorInfo[1]) ? $e->errorInfo[1] : null; - if ($this->db->driverName !== 'sqlsrv' || $errorCode !== $this->mssqlNoMoreRowsErrorCode) { + if ($this->getDbDriverName() !== 'sqlsrv' || $errorCode !== $this->mssqlNoMoreRowsErrorCode) { throw $e; } } @@ -202,4 +202,22 @@ class BatchQueryResult extends BaseObject implements \Iterator { return !empty($this->_batch); } + + /** + * Gets db driver name from the db connection that is passed to the `batch()`, if it is not passed it uses + * connection from the active record model + * @return string|null + */ + private function getDbDriverName() + { + if (isset($this->db->driverName)) { + return $this->db->driverName; + } + + if (isset($this->_batch[0]->db->driverName)) { + return $this->_batch[0]->db->driverName; + } + + return null; + } } diff --git a/tests/framework/db/BatchQueryResultTest.php b/tests/framework/db/BatchQueryResultTest.php index 2cdca486c1..24d9fdebd0 100644 --- a/tests/framework/db/BatchQueryResultTest.php +++ b/tests/framework/db/BatchQueryResultTest.php @@ -108,6 +108,16 @@ abstract class BatchQueryResultTest extends DatabaseTestCase $this->assertCount(0, $customers[2]->orders); } + public function testBatchWithoutDbParameter() + { + $query = Customer::find()->orderBy('id')->limit(3); + $customers = $this->getAllRowsFromBach($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) { $allRows = [];