mirror of
https://github.com/yiisoft/yii2.git
synced 2025-10-31 02:28:35 +08:00
Fix #20226: Revert all PR for "Data providers perform unnecessary COUNT queries that negatively affect performance"
This commit is contained in:
@ -7,8 +7,8 @@ Yii Framework 2 Change Log
|
||||
- Bug #20195: Do not set non abstract values into `ColumnSchema->type` on MSSQL version less then 2017 (axeltomasson)
|
||||
- Bug #16116: Codeception: oci does not support enabling/disabling integrity check (@terabytesoftw)
|
||||
- Bug #20191: Fix `ActiveRecord::getDirtyAttributes()` for JSON columns with multi-dimensional array values (brandonkelly)
|
||||
- Bug #20175: Fix bad result for pagination when used with GridView (@lav45)
|
||||
- Bug #20211: Add acceptable parameters to `MaskedInput::init()` method (alxlnk)
|
||||
- Bug #20226: Revert all PR for "Data providers perform unnecessary COUNT queries that negatively affect performance" (@terabytesoftw)
|
||||
|
||||
|
||||
2.0.50 May 30, 2024
|
||||
|
||||
@ -102,6 +102,7 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
}
|
||||
$query = clone $this->query;
|
||||
if (($pagination = $this->getPagination()) !== false) {
|
||||
$pagination->totalCount = $this->getTotalCount();
|
||||
if ($pagination->totalCount === 0) {
|
||||
return [];
|
||||
}
|
||||
@ -110,6 +111,7 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
if (($sort = $this->getSort()) !== false) {
|
||||
$query->addOrderBy($sort->getOrders());
|
||||
}
|
||||
|
||||
return $query->all($this->db);
|
||||
}
|
||||
|
||||
@ -127,6 +129,7 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
$keys[] = call_user_func($this->key, $model);
|
||||
}
|
||||
}
|
||||
|
||||
return $keys;
|
||||
} elseif ($this->query instanceof ActiveQueryInterface) {
|
||||
/* @var $class \yii\db\ActiveRecordInterface */
|
||||
@ -146,8 +149,10 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
$keys[] = $kk;
|
||||
}
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
return array_keys($models);
|
||||
}
|
||||
|
||||
@ -192,6 +197,7 @@ class ActiveDataProvider extends BaseDataProvider
|
||||
if (is_object($this->query)) {
|
||||
$this->query = clone $this->query;
|
||||
}
|
||||
|
||||
parent::__clone();
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,10 +86,14 @@ class ArrayDataProvider extends BaseDataProvider
|
||||
$models = $this->sortModels($models, $sort);
|
||||
}
|
||||
|
||||
$pagination = $this->getPagination();
|
||||
if ($pagination !== false && $pagination->getPageSize() > 0) {
|
||||
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
|
||||
if (($pagination = $this->getPagination()) !== false) {
|
||||
$pagination->totalCount = $this->getTotalCount();
|
||||
|
||||
if ($pagination->getPageSize() > 0) {
|
||||
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
|
||||
}
|
||||
}
|
||||
|
||||
return $models;
|
||||
}
|
||||
|
||||
|
||||
@ -164,13 +164,12 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa
|
||||
*/
|
||||
public function getTotalCount()
|
||||
{
|
||||
if ($this->_pagination === false) {
|
||||
if ($this->getPagination() === false) {
|
||||
return $this->getCount();
|
||||
} elseif ($this->_totalCount === null) {
|
||||
$this->_totalCount = $this->prepareTotalCount();
|
||||
}
|
||||
if ($this->_totalCount !== null) {
|
||||
return (int)$this->_totalCount;
|
||||
}
|
||||
return $this->prepareTotalCount();
|
||||
return $this->_totalCount;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -193,6 +192,7 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa
|
||||
if ($this->_pagination === null) {
|
||||
$this->setPagination([]);
|
||||
}
|
||||
|
||||
return $this->_pagination;
|
||||
}
|
||||
|
||||
@ -216,15 +216,9 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa
|
||||
$config['pageParam'] = $this->id . '-page';
|
||||
$config['pageSizeParam'] = $this->id . '-per-page';
|
||||
}
|
||||
$value = Yii::createObject(array_merge($config, $value));
|
||||
}
|
||||
if ($value instanceof Pagination) {
|
||||
$value->setTotalCount(function () {
|
||||
return $this->getTotalCount();
|
||||
});
|
||||
$this->_pagination = Yii::createObject(array_merge($config, $value));
|
||||
} elseif ($value instanceof Pagination || $value === false) {
|
||||
$this->_pagination = $value;
|
||||
} elseif ($value === false) {
|
||||
$this->_pagination = false;
|
||||
} else {
|
||||
throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.');
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
|
||||
namespace yii\data;
|
||||
|
||||
use Closure;
|
||||
use Yii;
|
||||
use yii\base\BaseObject;
|
||||
use yii\web\Link;
|
||||
@ -70,7 +69,6 @@ use yii\web\Request;
|
||||
* @property-read int $pageCount Number of pages.
|
||||
* @property int $pageSize The number of items per page. If it is less than 1, it means the page size is
|
||||
* infinite, and thus a single page contains all items.
|
||||
* @property int $totalCount total number of items.
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
@ -125,6 +123,10 @@ class Pagination extends BaseObject implements Linkable
|
||||
* number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]].
|
||||
*/
|
||||
public $validatePage = true;
|
||||
/**
|
||||
* @var int total number of items.
|
||||
*/
|
||||
public $totalCount = 0;
|
||||
/**
|
||||
* @var int the default page size. This property will be returned by [[pageSize]] when page size
|
||||
* cannot be determined by [[pageSizeParam]] from [[params]].
|
||||
@ -141,10 +143,6 @@ class Pagination extends BaseObject implements Linkable
|
||||
* If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
|
||||
*/
|
||||
private $_pageSize;
|
||||
/**
|
||||
* @var Closure|int total number of items or closure returning it.
|
||||
*/
|
||||
private $_totalCount = 0;
|
||||
|
||||
|
||||
/**
|
||||
@ -153,11 +151,13 @@ class Pagination extends BaseObject implements Linkable
|
||||
public function getPageCount()
|
||||
{
|
||||
$pageSize = $this->getPageSize();
|
||||
$totalCount = $this->getTotalCount();
|
||||
if ($pageSize < 1) {
|
||||
return $totalCount > 0 ? 1 : 0;
|
||||
return $this->totalCount > 0 ? 1 : 0;
|
||||
}
|
||||
return (int) ((max($totalCount, 0) + $pageSize - 1) / $pageSize);
|
||||
|
||||
$totalCount = $this->totalCount < 0 ? 0 : (int) $this->totalCount;
|
||||
|
||||
return (int) (($totalCount + $pageSize - 1) / $pageSize);
|
||||
}
|
||||
|
||||
private $_page;
|
||||
@ -173,6 +173,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
$page = (int) $this->getQueryParam($this->pageParam, 1) - 1;
|
||||
$this->setPage($page, true);
|
||||
}
|
||||
|
||||
return $this->_page;
|
||||
}
|
||||
|
||||
@ -220,6 +221,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
$this->setPageSize($pageSize, true);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_pageSize;
|
||||
}
|
||||
|
||||
@ -262,7 +264,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
$request = Yii::$app->getRequest();
|
||||
$params = $request instanceof Request ? $request->getQueryParams() : [];
|
||||
}
|
||||
if ($page > 0 || ($page === 0 && $this->forcePageParam)) {
|
||||
if ($page > 0 || $page == 0 && $this->forcePageParam) {
|
||||
$params[$this->pageParam] = $page + 1;
|
||||
} else {
|
||||
unset($params[$this->pageParam]);
|
||||
@ -280,6 +282,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
if ($absolute) {
|
||||
return $urlManager->createAbsoluteUrl($params);
|
||||
}
|
||||
|
||||
return $urlManager->createUrl($params);
|
||||
}
|
||||
|
||||
@ -290,6 +293,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
public function getOffset()
|
||||
{
|
||||
$pageSize = $this->getPageSize();
|
||||
|
||||
return $pageSize < 1 ? 0 : $this->getPage() * $pageSize;
|
||||
}
|
||||
|
||||
@ -301,6 +305,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
public function getLimit()
|
||||
{
|
||||
$pageSize = $this->getPageSize();
|
||||
|
||||
return $pageSize < 1 ? -1 : $pageSize;
|
||||
}
|
||||
|
||||
@ -326,6 +331,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
$links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, null, $absolute);
|
||||
}
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
@ -342,25 +348,7 @@ class Pagination extends BaseObject implements Linkable
|
||||
$request = Yii::$app->getRequest();
|
||||
$params = $request instanceof Request ? $request->getQueryParams() : [];
|
||||
}
|
||||
|
||||
return isset($params[$name]) && is_scalar($params[$name]) ? $params[$name] : $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int total number of items.
|
||||
*/
|
||||
public function getTotalCount()
|
||||
{
|
||||
if (is_numeric($this->_totalCount)) {
|
||||
return (int)$this->_totalCount;
|
||||
}
|
||||
return (int)call_user_func($this->_totalCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure|int $count
|
||||
*/
|
||||
public function setTotalCount($count)
|
||||
{
|
||||
$this->_totalCount = $count;
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,6 +126,7 @@ class SqlDataProvider extends BaseDataProvider
|
||||
}
|
||||
|
||||
if ($pagination !== false) {
|
||||
$pagination->totalCount = $this->getTotalCount();
|
||||
$limit = $pagination->getLimit();
|
||||
$offset = $pagination->getOffset();
|
||||
}
|
||||
@ -149,8 +150,10 @@ class SqlDataProvider extends BaseDataProvider
|
||||
$keys[] = call_user_func($this->key, $model);
|
||||
}
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
return array_keys($models);
|
||||
}
|
||||
|
||||
|
||||
@ -188,12 +188,6 @@ class Serializer extends Component
|
||||
*/
|
||||
protected function serializeDataProvider($dataProvider)
|
||||
{
|
||||
if (($pagination = $dataProvider->getPagination()) !== false) {
|
||||
$this->addPaginationHeaders($pagination);
|
||||
}
|
||||
if ($this->request->getIsHead()) {
|
||||
return null;
|
||||
}
|
||||
if ($this->preserveKeys) {
|
||||
$models = $dataProvider->getModels();
|
||||
} else {
|
||||
@ -201,7 +195,13 @@ class Serializer extends Component
|
||||
}
|
||||
$models = $this->serializeModels($models);
|
||||
|
||||
if ($this->collectionEnvelope === null) {
|
||||
if (($pagination = $dataProvider->getPagination()) !== false) {
|
||||
$this->addPaginationHeaders($pagination);
|
||||
}
|
||||
|
||||
if ($this->request->getIsHead()) {
|
||||
return null;
|
||||
} elseif ($this->collectionEnvelope === null) {
|
||||
return $models;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user