diff --git a/docs/en/04_Changelogs/4.3.0.md b/docs/en/04_Changelogs/4.3.0.md new file mode 100644 index 000000000..ca03e829f --- /dev/null +++ b/docs/en/04_Changelogs/4.3.0.md @@ -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. diff --git a/src/ORM/ArrayList.php b/src/ORM/ArrayList.php index a012d4009..952bbcc03 100644 --- a/src/ORM/ArrayList.php +++ b/src/ORM/ArrayList.php @@ -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 * diff --git a/src/ORM/DataList.php b/src/ORM/DataList.php index 097f830b1..bed403b1f 100644 --- a/src/ORM/DataList.php +++ b/src/ORM/DataList.php @@ -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 diff --git a/src/ORM/ListDecorator.php b/src/ORM/ListDecorator.php index 8d1397fcd..b141d548e 100644 --- a/src/ORM/ListDecorator.php +++ b/src/ORM/ListDecorator.php @@ -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); diff --git a/src/ORM/UnsavedRelationList.php b/src/ORM/UnsavedRelationList.php index 2885c89fc..5b1479b40 100644 --- a/src/ORM/UnsavedRelationList.php +++ b/src/ORM/UnsavedRelationList.php @@ -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. diff --git a/tests/php/Forms/GridField/GridFieldSortableHeaderTest.php b/tests/php/Forms/GridField/GridFieldSortableHeaderTest.php index c39ee1788..937731607 100644 --- a/tests/php/Forms/GridField/GridFieldSortableHeaderTest.php +++ b/tests/php/Forms/GridField/GridFieldSortableHeaderTest.php @@ -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); diff --git a/tests/php/ORM/DataQueryTest.php b/tests/php/ORM/DataQueryTest.php index b9278a496..ef3a72c96 100644 --- a/tests/php/ORM/DataQueryTest.php +++ b/tests/php/ORM/DataQueryTest.php @@ -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); + } }