mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Member::mapInGroups() throws SQL error
Renamed the Member::mapInGroups() to Member::map_in_groups() since it's a static method and throws deprecation message if using the old variant. Rewrote the mapInGroups to use a more ORMy way of fetching Members for a set of groups and included a test for.
This commit is contained in:
parent
7dcfdb05fa
commit
bbe3879eaa
@ -965,38 +965,55 @@ class Member extends DataObject implements TemplateGlobalProvider {
|
|||||||
return $map;
|
return $map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a member SQLMap of members in specific groups
|
||||||
|
*
|
||||||
|
* If no $groups is passed, all members will be returned
|
||||||
|
*
|
||||||
|
* @param mixed $groups - takes a SS_List, an array or a single Group.ID
|
||||||
|
* @return SQLMap Returns an SQLMap that returns all Member data.
|
||||||
|
* @see map()
|
||||||
|
*/
|
||||||
|
public static function mapInGroups($groups = null) {
|
||||||
|
Deprecation::notice('3.0', 'Use Member::map_in_groups() instead');
|
||||||
|
return self::map_in_groups();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a member SQLMap of members in specific groups
|
* Get a member SQLMap of members in specific groups
|
||||||
*
|
*
|
||||||
* @param mixed $groups Optional groups to include in the map. If NULL is
|
* If no $groups is passed, all members will be returned
|
||||||
* passed, all groups are returned, i.e.
|
*
|
||||||
* {@link map()} will be called.
|
* @param mixed $groups - takes a SS_List, an array or a single Group.ID
|
||||||
* @return SQLMap Returns an SQLMap that returns all Member data.
|
* @return SQLMap Returns an SQLMap that returns all Member data.
|
||||||
* @see map()
|
* @see map()
|
||||||
*
|
|
||||||
* @todo Improve documentation of this function! (Markus)
|
|
||||||
*/
|
*/
|
||||||
public static function mapInGroups($groups = null) {
|
public static function map_in_groups($groups = null) {
|
||||||
if(!$groups)
|
|
||||||
return Member::map();
|
|
||||||
|
|
||||||
$groupIDList = array();
|
$groupIDList = array();
|
||||||
|
|
||||||
if(is_a($groups, 'SS_List')) {
|
if($groups instanceof SS_List) {
|
||||||
foreach( $groups as $group )
|
foreach( $groups as $group ) {
|
||||||
$groupIDList[] = $group->ID;
|
$groupIDList[] = $group->ID;
|
||||||
|
}
|
||||||
} elseif(is_array($groups)) {
|
} elseif(is_array($groups)) {
|
||||||
$groupIDList = $groups;
|
$groupIDList = $groups;
|
||||||
} else {
|
} elseif($groups) {
|
||||||
$groupIDList[] = $groups;
|
$groupIDList[] = $groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(empty($groupIDList))
|
// No groups, return all Members
|
||||||
return Member::map();
|
if(!$groupIDList) {
|
||||||
|
return Member::get()->sort(array('Surname'=>'ASC', 'FirstName'=>'ASC'))->map();
|
||||||
|
}
|
||||||
|
|
||||||
return DataList::create("Member")->where("\"GroupID\" IN (" . implode( ',', $groupIDList ) . ")")
|
$membersList = new ArrayList();
|
||||||
->sort("\"Surname\", \"FirstName\"")->map();
|
// This is a bit ineffective, but follow the ORM style
|
||||||
|
foreach(Group::get()->byIDs($groupIDList) as $group) {
|
||||||
|
$membersList->merge($group->Members());
|
||||||
|
}
|
||||||
|
|
||||||
|
$membersList->removeDuplicates('ID');
|
||||||
|
return $membersList->map();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -565,6 +565,29 @@ class MemberTest extends FunctionalTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that all members are returned
|
||||||
|
*/
|
||||||
|
function testMap_in_groupsReturnsAll() {
|
||||||
|
$members = Member::map_in_groups();
|
||||||
|
$this->assertEquals(13, count($members), 'There are 12 members in the mock plus a fake admin');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that only admin members are returned
|
||||||
|
*/
|
||||||
|
function testMap_in_groupsReturnsAdmins() {
|
||||||
|
$adminID = $this->objFromFixture('Group', 'admingroup')->ID;
|
||||||
|
$members = Member::map_in_groups($adminID);
|
||||||
|
|
||||||
|
$admin = $this->objFromFixture('Member', 'admin');
|
||||||
|
$otherAdmin = $this->objFromFixture('Member', 'other-admin');
|
||||||
|
|
||||||
|
$this->assertTrue(in_array($admin->getTitle(), $members), $admin->getTitle().' should be in the returned list.');
|
||||||
|
$this->assertTrue(in_array($otherAdmin->getTitle(), $members), $otherAdmin->getTitle().' should be in the returned list.');
|
||||||
|
$this->assertEquals(2, count($members), 'There should be 2 members from the admin group');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the given array of member extensions as class names.
|
* Add the given array of member extensions as class names.
|
||||||
* This is useful for re-adding extensions after being removed
|
* This is useful for re-adding extensions after being removed
|
||||||
|
Loading…
Reference in New Issue
Block a user