mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Add columnUnique
API SS_List classes.
This commit is contained in:
parent
91068c23b5
commit
3292a8b773
@ -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
|
||||
*
|
||||
|
@ -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
|
||||
|
@ -1147,7 +1147,6 @@ class DataQuery
|
||||
{
|
||||
$fieldExpression = $this->expressionForField($field);
|
||||
$query = $this->getFinalisedQuery(array($field));
|
||||
$query->setDistinct(false);
|
||||
$originalSelect = $query->getSelect();
|
||||
$query->setSelect(array());
|
||||
$query->selectField($fieldExpression, $field);
|
||||
|
@ -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);
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
|
||||
|
@ -397,4 +397,24 @@ class DataQueryTest extends SapphireTest
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user