Skip to content

Commit

Permalink
Fix #20226: Revert all PR for "Data providers perform unnecessary COU…
Browse files Browse the repository at this point in the history
…NT queries that negatively affect performance"
  • Loading branch information
terabytesoftw authored Jul 12, 2024
1 parent 5f5ef64 commit a6257d8
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 158 deletions.
70 changes: 36 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions framework/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ protected function prepareModels()
}
$query = clone $this->query;
if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();
if ($pagination->totalCount === 0) {
return [];
}
Expand All @@ -110,6 +111,7 @@ protected function prepareModels()
if (($sort = $this->getSort()) !== false) {
$query->addOrderBy($sort->getOrders());
}

return $query->all($this->db);
}

Expand All @@ -127,6 +129,7 @@ protected function prepareKeys($models)
$keys[] = call_user_func($this->key, $model);
}
}

return $keys;
} elseif ($this->query instanceof ActiveQueryInterface) {
/* @var $class \yii\db\ActiveRecordInterface */
Expand All @@ -146,8 +149,10 @@ protected function prepareKeys($models)
$keys[] = $kk;
}
}

return $keys;
}

return array_keys($models);
}

Expand Down Expand Up @@ -192,6 +197,7 @@ public function __clone()
if (is_object($this->query)) {
$this->query = clone $this->query;
}

parent::__clone();
}
}
10 changes: 7 additions & 3 deletions framework/data/ArrayDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ protected function prepareModels()
$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;
}

Expand Down
20 changes: 7 additions & 13 deletions framework/data/BaseDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,12 @@ public function getCount()
*/
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;
}

/**
Expand All @@ -193,6 +192,7 @@ public function getPagination()
if ($this->_pagination === null) {
$this->setPagination([]);
}

return $this->_pagination;
}

Expand All @@ -216,15 +216,9 @@ public function setPagination($value)
$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.');
}
Expand Down
Loading

0 comments on commit a6257d8

Please sign in to comment.