Permalink
Browse files

FIX: Remove limit on GridField export

Allow DataList::limit() to take a null value to remove the limit.

Added tests for limit(). Note the one failure, currently the ORM doesn't support unlimited values with an offset.
  • Loading branch information...
1 parent c544281 commit 65d96e8d7ceb3a38af67b8dc6722e6dfd2a86643 @wilr wilr committed Aug 5, 2013
Showing with 34 additions and 6 deletions.
  1. +2 −1 forms/gridfield/GridFieldExportButton.php
  2. +5 −0 model/ArrayList.php
  3. +4 −5 model/DataList.php
  4. +23 −0 tests/model/DataListTest.php
@@ -128,8 +128,9 @@ public function generateExportFileData($gridField) {
}
}
- foreach($items as $item) {
+ foreach($items->limit(null) as $item) {
$columnData = array();
+
foreach($csvColumns as $columnSource => $columnHeader) {
if(!is_string($columnHeader) && is_callable($columnHeader)) {
if($item->hasMethod($columnSource)) {
View
@@ -133,8 +133,13 @@ public function toNestedArray() {
* @return ArrayList
*/
public function limit($length, $offset = 0) {
+ if(!$length) {
+ $length = count($this->items);
+ }
+
$list = clone $this;
$list->items = array_slice($this->items, $offset, $length);
+
return $list;
}
View
@@ -212,22 +212,21 @@ public function canFilterBy($fieldName) {
}
/**
- * Return a new DataList instance with the records returned in this query restricted by a limit clause
+ * Return a new DataList instance with the records returned in this query
+ * restricted by a limit clause.
*
* @param int $limit
* @param int $offset
*/
public function limit($limit, $offset = 0) {
- if(!$limit && !$offset) {
- return $this;
- }
return $this->alterDataQuery(function($query) use ($limit, $offset){
$query->limit($limit, $offset);
});
}
/**
- * Return a new DataList instance as a copy of this data list with the sort order set
+ * Return a new DataList instance as a copy of this data list with the sort
+ * order set.
*
* @see SS_List::sort()
* @see SQLQuery::orderby
@@ -98,6 +98,29 @@ public function testListCreationSortAndLimit() {
// We can also restrict the output to a range
$this->assertEquals(array('Joe', 'Phil'), $list->limit(2, 1)->column('Name'));
}
+
+ public function testLimitAndOffset() {
+ $list = DataObjectTest_TeamComment::get();
+ $check = $list->limit(3);
+
+ $this->assertEquals(3, $check->count());
+
+ $check = $list->limit(1);
+ $this->assertEquals(1, $check->count());
+
+ $check = $list->limit(1, 1);
+ $this->assertEquals(1, $check->count());
+
+ $check = $list->limit(false);
+ $this->assertEquals(3, $check->count());
+
+ $check = $list->limit(null);
+ $this->assertEquals(3, $check->count());
+
+ // no limit with an offset is currently not supported by the orm
+ // $check = $list->limit(null, 2);
+ // $this->assertEquals(1, $check->count());
+ }
public function testDataClass() {
$list = DataObjectTest_TeamComment::get();

0 comments on commit 65d96e8

Please sign in to comment.