Merge pull request #8244 from altwohill/column-not-distinct

Make column query not distinct
This commit is contained in:
Daniel Hensby 2018-07-16 13:01:32 +01:00 committed by GitHub
commit 1a634f5ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 2 deletions

View File

@ -0,0 +1,13 @@
# 4.2.0
## Overview {#overview}
- `DataList::column()` now returns all values and not just "distinct" values from a column as per the API docs
- `DataList`, `ArrayList` and `UnsavedRalationList` all have `columnUnique()` method for fetching distinct column values
## Upgrading {#upgrading}
### Fetching distinct column values on DataList
Prior to this release `DataList` would erroneously return a distinct list of values from a column on an object.
If this behaviour is still required, please use `columnUnique()` instead.

View File

@ -370,6 +370,17 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L
return $result;
}
/**
* Returns a unique array of a single field value for all the items in the list
*
* @param string $colName
* @return array
*/
public function columnUnique($colName = 'ID')
{
return array_unique($this->column($colName));
}
/**
* You can always sort a ArrayList
*

View File

@ -990,7 +990,18 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*/
public function column($colName = "ID")
{
return $this->dataQuery->column($colName);
return $this->dataQuery->distinct(false)->column($colName);
}
/**
* Returns a unque array of a single field value for all items in the list.
*
* @param string $colName
* @return array
*/
public function columnUnique($colName = "ID")
{
return $this->dataQuery->distinct(true)->column($colName);
}
// Member altering methods

View File

@ -142,6 +142,11 @@ abstract class ListDecorator extends ViewableData implements SS_List, Sortable,
return $this->list->column($value);
}
public function columnUnique($value = "ID")
{
return $this->list->columnUnique($value);
}
public function each($callback)
{
return $this->list->each($callback);

View File

@ -275,6 +275,18 @@ class UnsavedRelationList extends ArrayList implements Relation
return $list->column($colName);
}
/**
* Returns a unique array of a single field value for all items in the list.
*
* @param string $colName
* @return array
*/
public function columnUnique($colName = "ID")
{
$list = new ArrayList($this->toArray());
return $list->columnUnique($colName);
}
/**
* Returns a copy of this list with the relationship linked to the given foreign ID.
* @param int|array $id An ID or an array of IDs.

View File

@ -68,7 +68,7 @@ class GridFieldSortableHeaderTest extends SapphireTest
public function testGetManipulatedData()
{
$list = new DataList(Team::class);
$list = Team::get()->filter([ 'ClassName' => Team::class ]);
$config = new GridFieldConfig_RecordEditor();
$gridField = new GridField('testfield', 'testfield', $list, $config);

View File

@ -379,4 +379,42 @@ class DataQueryTest extends SapphireTest
$this->assertEquals('Last', $second['Title']);
$this->assertEmpty(array_shift($arrayResult));
}
public function testColumnReturnsAllValues()
{
$first = new DataQueryTest\ObjectA();
$first->Name = 'Bar';
$first->write();
$second = new DataQueryTest\ObjectA();
$second->Name = 'Foo';
$second->write();
$third = new DataQueryTest\ObjectA();
$third->Name = 'Bar';
$third->write();
$result = DataQueryTest\ObjectA::get()->column('Name');
$this->assertEquals(['Bar', 'Foo', 'Bar'], $result);
}
public function testColumnUniqueReturnsAllValues()
{
$first = new DataQueryTest\ObjectA();
$first->Name = 'Bar';
$first->write();
$second = new DataQueryTest\ObjectA();
$second->Name = 'Foo';
$second->write();
$third = new DataQueryTest\ObjectA();
$third->Name = 'Bar';
$third->write();
$result = DataQueryTest\ObjectA::get()->columnUnique('Name');
$this->assertCount(2, $result);
$this->assertContains('Bar', $result);
$this->assertContains('Foo', $result);
}
}