mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Allow DataObjectSet to remove duplicates based on any field (#5094, thanks mobiusnz)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@99736 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
ddbc3e96b3
commit
a4e14d3c56
@ -855,16 +855,17 @@ class DataObjectSet extends ViewableData implements IteratorAggregate, Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove duplicates from this set based on the dataobjects ID.
|
* Remove duplicates from this set based on the dataobjects field.
|
||||||
* Assumes all items contained in the set all have IDs.
|
* Assumes all items contained in the set all have that field.
|
||||||
|
* @param string $field the field to check for duplicates
|
||||||
*/
|
*/
|
||||||
public function removeDuplicates() {
|
public function removeDuplicates($field = 'ID') {
|
||||||
$exists = array();
|
$exists = array();
|
||||||
foreach($this->items as $key => $item) {
|
foreach($this->items as $key => $item) {
|
||||||
if(isset($exists[$item->ID])) {
|
if(isset($exists[$item->$field])) {
|
||||||
unset($this->items[$key]);
|
unset($this->items[$key]);
|
||||||
}
|
}
|
||||||
$exists[$item->ID] = true;
|
$exists[$item->$field] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,5 +186,46 @@ class DataObjectSetTest extends SapphireTest {
|
|||||||
$this->assertEquals(count($map), 9, 'There are 9 items in the map. 8 are records. 1 is the empty value.');
|
$this->assertEquals(count($map), 9, 'There are 9 items in the map. 8 are records. 1 is the empty value.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testRemoveDuplicates() {
|
||||||
|
$pageComments = DataObject::get('PageComment');
|
||||||
|
$teamComments = DataObject::get('DataObjectSetTest_TeamComment');
|
||||||
|
|
||||||
|
/* Test default functionality (remove by ID). We'd expect to loose all our
|
||||||
|
* team comments as they have the same IDs as the first three page comments */
|
||||||
|
|
||||||
|
$allComments = new DataObjectSet();
|
||||||
|
$allComments->merge($pageComments);
|
||||||
|
$allComments->merge($teamComments);
|
||||||
|
|
||||||
|
$allComments->removeDuplicates();
|
||||||
|
|
||||||
|
$this->assertEquals($allComments->Count(), 8, 'Standard functionality is to remove duplicate IDs');
|
||||||
|
|
||||||
|
/* Now test removing duplicates based on a common field. In this case we shall
|
||||||
|
* use 'Name', so we can get all the unique commentators */
|
||||||
|
|
||||||
|
|
||||||
|
$allComments = new DataObjectSet();
|
||||||
|
$allComments->merge($pageComments);
|
||||||
|
$allComments->merge($teamComments);
|
||||||
|
|
||||||
|
$allComments->removeDuplicates('Name');
|
||||||
|
|
||||||
|
$this->assertEquals($allComments->Count(), 7, 'There are 7 uniquely named commentators');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
class DataObjectSetTest_TeamComment extends DataObject implements TestOnly {
|
||||||
|
static $db = array(
|
||||||
|
'Name' => 'Varchar',
|
||||||
|
'Comment' => 'Text',
|
||||||
|
);
|
||||||
|
static $has_one = array(
|
||||||
|
'Team' => 'DataObjectTest_Team',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -66,3 +66,16 @@ DataObjectTest_SubTeam:
|
|||||||
DecoratedHasOneRelationship: =>DataObjectTest_Player.player1
|
DecoratedHasOneRelationship: =>DataObjectTest_Player.player1
|
||||||
subteam3_with_empty_fields:
|
subteam3_with_empty_fields:
|
||||||
Title: Subteam 3
|
Title: Subteam 3
|
||||||
|
DataObjectSetTest_TeamComment:
|
||||||
|
comment1:
|
||||||
|
Name: Joe
|
||||||
|
Comment: This is a team comment by Joe
|
||||||
|
Team: =>DataObjectTest_Team.team1
|
||||||
|
comment2:
|
||||||
|
Name: Bob
|
||||||
|
Comment: This is a team comment by Bob
|
||||||
|
Team: =>DataObjectTest_Team.team1
|
||||||
|
comment3:
|
||||||
|
Name: Phil
|
||||||
|
Comment: Phil is a unique guy, and comments on team2
|
||||||
|
Tema: =>DataObjectTest_Team.team2
|
Loading…
Reference in New Issue
Block a user