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.
This commit is contained in:
Will Rossiter 2013-08-05 19:59:12 +12:00
parent c5442810cf
commit 65d96e8d7c
4 changed files with 34 additions and 6 deletions

View File

@ -128,8 +128,9 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
} }
} }
foreach($items as $item) { foreach($items->limit(null) as $item) {
$columnData = array(); $columnData = array();
foreach($csvColumns as $columnSource => $columnHeader) { foreach($csvColumns as $columnSource => $columnHeader) {
if(!is_string($columnHeader) && is_callable($columnHeader)) { if(!is_string($columnHeader) && is_callable($columnHeader)) {
if($item->hasMethod($columnSource)) { if($item->hasMethod($columnSource)) {

View File

@ -133,8 +133,13 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
* @return ArrayList * @return ArrayList
*/ */
public function limit($length, $offset = 0) { public function limit($length, $offset = 0) {
if(!$length) {
$length = count($this->items);
}
$list = clone $this; $list = clone $this;
$list->items = array_slice($this->items, $offset, $length); $list->items = array_slice($this->items, $offset, $length);
return $list; return $list;
} }

View File

@ -212,22 +212,21 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
} }
/** /**
* 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 $limit
* @param int $offset * @param int $offset
*/ */
public function limit($limit, $offset = 0) { public function limit($limit, $offset = 0) {
if(!$limit && !$offset) {
return $this;
}
return $this->alterDataQuery(function($query) use ($limit, $offset){ return $this->alterDataQuery(function($query) use ($limit, $offset){
$query->limit($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 SS_List::sort()
* @see SQLQuery::orderby * @see SQLQuery::orderby

View File

@ -99,6 +99,29 @@ class DataListTest extends SapphireTest {
$this->assertEquals(array('Joe', 'Phil'), $list->limit(2, 1)->column('Name')); $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() { public function testDataClass() {
$list = DataObjectTest_TeamComment::get(); $list = DataObjectTest_TeamComment::get();
$this->assertEquals('DataObjectTest_TeamComment',$list->dataClass()); $this->assertEquals('DataObjectTest_TeamComment',$list->dataClass());