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

This commit is contained in:
Nikolay Oleynikov
2017-02-20 12:00:00 +03:00
committed by Alexander Makarov
parent ecd2dc0d1b
commit 7d82bbcd37
4 changed files with 50 additions and 0 deletions

View File

@ -18,6 +18,7 @@ Yii Framework 2 Change Log
- Bug #13592: Fixes Oracles `yii\db\oci\Schema::setTransactionIsolationLevel()` (sergeymakinen) - Bug #13592: Fixes Oracles `yii\db\oci\Schema::setTransactionIsolationLevel()` (sergeymakinen)
- Bug #13594: Fixes insufficient quoting in `yii\db\QueryBuilder::prepareInsertSelectSubQuery()` (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 #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 2.0.11.2 February 08, 2017

View File

@ -104,6 +104,9 @@ class ActiveDataProvider extends BaseDataProvider
$query = clone $this->query; $query = clone $this->query;
if (($pagination = $this->getPagination()) !== false) { if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount(); $pagination->totalCount = $this->getTotalCount();
if ($pagination->totalCount === 0) {
return [];
}
$query->limit($pagination->getLimit())->offset($pagination->getOffset()); $query->limit($pagination->getLimit())->offset($pagination->getOffset());
} }
if (($sort = $this->getSort()) !== false) { if (($sort = $this->getSort()) !== false) {

View File

@ -7,12 +7,14 @@
namespace yiiunit\framework\data; namespace yiiunit\framework\data;
use yii\base\InvalidCallException;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use yii\db\Query; use yii\db\Query;
use yiiunit\data\ar\ActiveRecord; use yiiunit\data\ar\ActiveRecord;
use yiiunit\data\ar\Customer; use yiiunit\data\ar\Customer;
use yiiunit\data\ar\Item; use yiiunit\data\ar\Item;
use yiiunit\framework\db\DatabaseTestCase; use yiiunit\framework\db\DatabaseTestCase;
use yiiunit\framework\db\UnqueryableQueryMock;
use yiiunit\data\ar\Order; use yiiunit\data\ar\Order;
/** /**
@ -177,4 +179,23 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase
$provider->refresh(); $provider->refresh();
$this->assertEquals(2, count($provider->getModels())); $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());
}
} }

View File

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