mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #8244 from altwohill/column-not-distinct
Make column query not distinct
This commit is contained in:
commit
1a634f5ba6
13
docs/en/04_Changelogs/4.3.0.md
Normal file
13
docs/en/04_Changelogs/4.3.0.md
Normal 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.
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user