ensure gridview query is reusable

apply pagination and sorting to a clone of it.
This commit is contained in:
Carsten Brandt
2014-04-22 15:47:52 +02:00
parent 0a3f4e85f1
commit f6f0e66598
2 changed files with 6 additions and 4 deletions

View File

@ -29,6 +29,7 @@ Yii Framework 2 Change Log
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
- Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue)
- Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe)
- Chg: `yii\data\ActiveDataProvider::$query` will not be modified directly with pagination and sorting anymore so it will be reuseable (cebe)
2.0.0-beta April 13, 2014

View File

@ -77,6 +77,7 @@ class ActiveDataProvider extends BaseDataProvider
*/
public $db;
/**
* Initializes the DB connection component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
@ -98,15 +99,16 @@ class ActiveDataProvider extends BaseDataProvider
if (!$this->query instanceof QueryInterface) {
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
}
$query = clone $this->query;
if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();
$this->query->limit($pagination->getLimit())->offset($pagination->getOffset());
$query->limit($pagination->getLimit())->offset($pagination->getOffset());
}
if (($sort = $this->getSort()) !== false) {
$this->query->addOrderBy($sort->getOrders());
$query->addOrderBy($sort->getOrders());
}
return $this->query->all($this->db);
return $query->all($this->db);
}
/**
@ -159,7 +161,6 @@ class ActiveDataProvider extends BaseDataProvider
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
}
$query = clone $this->query;
return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db);
}