mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT DataObjectSet::groupBy() now supports use of a method name to group objects
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@74996 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
235ce958b8
commit
16293cf1d4
@ -685,15 +685,19 @@ class DataObjectSet extends ViewableData implements IteratorAggregate {
|
||||
|
||||
/**
|
||||
* Returns an array of DataObjectSets. The array is keyed by index.
|
||||
*
|
||||
* @param string $index The field name to index the array by.
|
||||
* @return array
|
||||
*/
|
||||
public function groupBy($index) {
|
||||
$result = array();
|
||||
foreach($this->items as $item) {
|
||||
if(!isset($result[$item->$index])) {
|
||||
$result[$item->$index] = new DataObjectSet();
|
||||
$key = ($item->hasMethod($index)) ? $item->$index() : $item->$index;
|
||||
|
||||
if(!isset($result[$key])) {
|
||||
$result[$key] = new DataObjectSet();
|
||||
}
|
||||
$result[$item->$index]->push($item);
|
||||
$result[$key]->push($item);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user