Commit f6f0e665 by Carsten Brandt

ensure gridview query is reusable

apply pagination and sorting to a clone of it.
parent 0a3f4e85
...@@ -29,6 +29,7 @@ Yii Framework 2 Change Log ...@@ -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: 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: 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\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 2.0.0-beta April 13, 2014
......
...@@ -77,6 +77,7 @@ class ActiveDataProvider extends BaseDataProvider ...@@ -77,6 +77,7 @@ class ActiveDataProvider extends BaseDataProvider
*/ */
public $db; public $db;
/** /**
* Initializes the DB connection component. * Initializes the DB connection component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection. * 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 ...@@ -98,15 +99,16 @@ class ActiveDataProvider extends BaseDataProvider
if (!$this->query instanceof QueryInterface) { 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.'); 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) { if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount(); $pagination->totalCount = $this->getTotalCount();
$this->query->limit($pagination->getLimit())->offset($pagination->getOffset()); $query->limit($pagination->getLimit())->offset($pagination->getOffset());
} }
if (($sort = $this->getSort()) !== false) { 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 ...@@ -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.'); 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; $query = clone $this->query;
return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db); return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment