NEW Add columnUnique API SS_List classes.

This commit is contained in:
Al Twohill 2018-07-12 13:44:32 +12:00 committed by Daniel Hensby
parent 91068c23b5
commit 3292a8b773
No known key found for this signature in database
GPG Key ID: D8DEBC4C8E7BC8B9
7 changed files with 61 additions and 3 deletions

View File

@ -370,6 +370,17 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L
return $result; 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 * 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") 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 // Member altering methods

View File

@ -1147,7 +1147,6 @@ class DataQuery
{ {
$fieldExpression = $this->expressionForField($field); $fieldExpression = $this->expressionForField($field);
$query = $this->getFinalisedQuery(array($field)); $query = $this->getFinalisedQuery(array($field));
$query->setDistinct(false);
$originalSelect = $query->getSelect(); $originalSelect = $query->getSelect();
$query->setSelect(array()); $query->setSelect(array());
$query->selectField($fieldExpression, $field); $query->selectField($fieldExpression, $field);

View File

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

View File

@ -275,6 +275,18 @@ class UnsavedRelationList extends ArrayList implements Relation
return $list->column($colName); 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. * 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. * @param int|array $id An ID or an array of IDs.

View File

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

View File

@ -397,4 +397,24 @@ class DataQueryTest extends SapphireTest
$result = DataQueryTest\ObjectA::get()->column('Name'); $result = DataQueryTest\ObjectA::get()->column('Name');
$this->assertEquals(['Bar', 'Foo', 'Bar'], $result); $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);
}
} }