diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6716897..27672f3 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -18,6 +18,7 @@ Yii Framework 2 Change Log - Bug #13592: Fixes Oracle’s `yii\db\oci\Schema::setTransactionIsolationLevel()` (sergeymakinen) - Bug #13594: Fixes insufficient quoting in `yii\db\QueryBuilder::prepareInsertSelectSubQuery()` (sergeymakinen) - Enh #13576: Added support of `srcset` to `yii\helpers\Html::img()` (Kolyunya) +- Enh #13467: `yii\data\ActiveDataProvider` no longer queries models if models count is zero (kLkA, Kolyunya) 2.0.11.2 February 08, 2017 diff --git a/framework/data/ActiveDataProvider.php b/framework/data/ActiveDataProvider.php index 3b4aecd..691400b 100644 --- a/framework/data/ActiveDataProvider.php +++ b/framework/data/ActiveDataProvider.php @@ -104,6 +104,9 @@ class ActiveDataProvider extends BaseDataProvider $query = clone $this->query; if (($pagination = $this->getPagination()) !== false) { $pagination->totalCount = $this->getTotalCount(); + if ($pagination->totalCount === 0) { + return []; + } $query->limit($pagination->getLimit())->offset($pagination->getOffset()); } if (($sort = $this->getSort()) !== false) { diff --git a/tests/framework/data/ActiveDataProviderTest.php b/tests/framework/data/ActiveDataProviderTest.php index 94f52e8..ae74ee5 100644 --- a/tests/framework/data/ActiveDataProviderTest.php +++ b/tests/framework/data/ActiveDataProviderTest.php @@ -7,12 +7,14 @@ namespace yiiunit\framework\data; +use yii\base\InvalidCallException; use yii\data\ActiveDataProvider; use yii\db\Query; use yiiunit\data\ar\ActiveRecord; use yiiunit\data\ar\Customer; use yiiunit\data\ar\Item; use yiiunit\framework\db\DatabaseTestCase; +use yiiunit\framework\db\UnqueryableQueryMock; use yiiunit\data\ar\Order; /** @@ -177,4 +179,23 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase $provider->refresh(); $this->assertEquals(2, count($provider->getModels())); } + + public function testDoesNotPerformQueryWhenHasNoModels() + { + $query = new UnqueryableQueryMock; + $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), + 'query' => $query->from('order')->where('0=1'), + ]); + $pagination = $provider->getPagination(); + $this->assertEquals(0, $pagination->getPageCount()); + + try { + $this->assertCount(0, $provider->getModels()); + } catch (InvalidCallException $exception) { + $this->fail('An excessive models query was executed.'); + } + + $this->assertEquals(0, $pagination->getPageCount()); + } } diff --git a/tests/framework/db/UnqueryableQueryMock.php b/tests/framework/db/UnqueryableQueryMock.php new file mode 100644 index 0000000..87172be --- /dev/null +++ b/tests/framework/db/UnqueryableQueryMock.php @@ -0,0 +1,25 @@ +