Browse Source

Fixes #13883: `\yii\data\SqlDataProvider` now provides automatic fallback for the case when `totalCount` is not specified

tags/2.0.12
Sam Mousa 8 years ago committed by Alexander Makarov
parent
commit
79f16b1492
No known key found for this signature in database
GPG Key ID: 3617B79C6A325E4A
  1. 1
      framework/CHANGELOG.md
  2. 3
      framework/data/SqlDataProvider.php
  3. 35
      tests/framework/data/SqlDataProviderTest.php

1
framework/CHANGELOG.md

@ -55,6 +55,7 @@ Yii Framework 2 Change Log
- Bug #13704: Fixed `yii\validators\UniqueValidator` to prefix attribute name with model's database table name (vladis84)
- Enh #13823: Refactored migrations template (Kolyunya)
- Enh #13845: `mt_rand()` is used instead of `rand()` in `yii\captcha\CaptchaAction` (kalessil)
- Enh #13883: `\yii\data\SqlDataProvider` now provides automatic fallback for the case when `totalCount` is not specified (SamMousa)
2.0.11.2 February 08, 2017
--------------------------

3
framework/data/SqlDataProvider.php

@ -11,6 +11,7 @@ use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Expression;
use yii\db\Query;
use yii\di\Instance;
/**
@ -162,6 +163,6 @@ class SqlDataProvider extends BaseDataProvider
*/
protected function prepareTotalCount()
{
return 0;
return (new Query())->from(['sub' => "({$this->sql})"])->count('*', $this->db);
}
}

35
tests/framework/data/SqlDataProviderTest.php

@ -0,0 +1,35 @@
<?php
namespace yiiunit\framework\data;
use yii\data\ArrayDataProvider;
use yii\data\SqlDataProvider;
use yiiunit\framework\db\DatabaseTestCase;
use yiiunit\framework\db\sqlite\ConnectionTest;
use yiiunit\TestCase;
/**
* @group data
*/
class SqlDataProviderTest extends DatabaseTestCase
{
protected $driverName = 'sqlite';
public function testGetModels()
{
$dataProvider = new SqlDataProvider([
'sql' => 'select * from `customer`',
'db' => $this->getConnection(),
]);
$this->assertCount(3, $dataProvider->getModels());
}
public function testTotalCount()
{
$dataProvider = new SqlDataProvider([
'sql' => 'select * from `customer`',
'db' => $this->getConnection(),
]);
$this->assertEquals(3, $dataProvider->getTotalCount());
}
}
Loading…
Cancel
Save