From f6f0e66598d956b13bd52674ef39a6db94e67b97 Mon Sep 17 00:00:00 2001
From: Carsten Brandt <mail@cebe.cc>
Date: Tue, 22 Apr 2014 15:47:52 +0200
Subject: [PATCH] ensure gridview query is reusable

apply pagination and sorting to a clone of it.
---
 framework/CHANGELOG.md                |  1 +
 framework/data/ActiveDataProvider.php |  9 +++++----
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md
index 44a1f98..9ae93e5 100644
--- a/framework/CHANGELOG.md
+++ b/framework/CHANGELOG.md
@@ -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
diff --git a/framework/data/ActiveDataProvider.php b/framework/data/ActiveDataProvider.php
index 6a782fa..60f4334 100644
--- a/framework/data/ActiveDataProvider.php
+++ b/framework/data/ActiveDataProvider.php
@@ -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);
     }
 
--
libgit2 0.27.1