Browse Source

Fixes #13467: `yii\data\ActiveDataProvider` no longer queries models if models count is zero

tags/2.0.12
Nikolay Oleynikov 8 years ago committed by Alexander Makarov
parent
commit
7d82bbcd37
  1. 1
      framework/CHANGELOG.md
  2. 3
      framework/data/ActiveDataProvider.php
  3. 21
      tests/framework/data/ActiveDataProviderTest.php
  4. 25
      tests/framework/db/UnqueryableQueryMock.php

1
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

3
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) {

21
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());
}
}

25
tests/framework/db/UnqueryableQueryMock.php

@ -0,0 +1,25 @@
<?php
namespace yiiunit\framework\db;
use yii\base\InvalidCallException;
use yii\db\Query;
class UnqueryableQueryMock extends Query
{
/**
* @inheritdoc
*/
public function one($db = null)
{
throw new InvalidCallException();
}
/**
* @inheritdoc
*/
public function all($db = null)
{
throw new InvalidCallException();
}
}
Loading…
Cancel
Save