ENHANCEMENT Allow DataObjectSet to remove duplicates based on any field (#5094, thanks mobiusnz) (from r99736)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@99737 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-02-23 22:18:44 +00:00 committed by Sam Minnee
parent d6b219957a
commit 578aa21de9
3 changed files with 61 additions and 5 deletions

View File

@ -826,16 +826,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;
} }
} }

View File

@ -13,6 +13,7 @@ class DataObjectSetTest extends SapphireTest {
'DataObjectTest_Team', 'DataObjectTest_Team',
'DataObjectTest_SubTeam', 'DataObjectTest_SubTeam',
'DataObjectTest_Player', 'DataObjectTest_Player',
'DataObjectSetTest_TeamComment'
); );
function testIterator() { function testIterator() {
@ -191,5 +192,46 @@ class DataObjectSetTest extends SapphireTest {
//$this->assertSame($expectedMap, $map2, 'The map we generated is exactly the same as the asserted one'); //$this->assertSame($expectedMap, $map2, 'The map we generated is exactly the same as the asserted one');
} }
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',
);
} }
?> ?>

View File

@ -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