Skip to content

Commit

Permalink
ensure gridview query is reusable
Browse files Browse the repository at this point in the history
apply pagination and sorting to a clone of it.
  • Loading branch information
cebe committed Apr 22, 2014
1 parent 0a3f4e8 commit f6f0e66
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions framework/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -98,15 +99,16 @@ protected function prepareModels()
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);
}

/**
Expand Down Expand Up @@ -159,7 +161,6 @@ protected function prepareTotalCount()
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);
}

Expand Down

0 comments on commit f6f0e66

Please sign in to comment.