Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

want to remove members from group but no errors nor any results


Go to End


3 Posts   2131 Views

Avatar
vancouverWill

Community Member, 121 Posts

6 August 2009 at 1:24pm

Hi All

I have written up a piece of code which should run very straightforwards. I first select all members, then cycling through them I create an array of members which are in group 8( the one I want to remove my members from). I should then be able to either as the cycle goes through remove the membership or cycle through the new array and remove the membership then. Fairly simple I think but no luck whatsoever. Also if it helps


<?php

class ClearExpiredUsersTask extends HourlyTask {

    function process() {

$allExpiredMembers = array();
$ExpiredUsersGroup = DataObject::get_one('Group', "Code = 'expired-users'");

      foreach($members as $member) {

if($member->inGroup("8")){
		$allExpiredMembers[$member->ID] = htmlentities($member->Email);
					//$expiredMember = DataObject::get_by_id("Member", $member->ID);
					//$expiredMember->Groups()->remove($ExpiredUsersGroup);	
}
      }

//////////not necessary but shows what is going on ///////////////////////////////////////////

print_r($allExpiredMembers);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			foreach($allExpiredMembers as $member=>$Email)	
				{
					$expiredMember = DataObject::get_one("Member", "Email = '$Email'");
					$expiredMember->Groups()->remove(DataObject::get_one('Group', $ExpiredUsersGroup));	
				}
    }
}

?>
[\code]

Any tips would be really appreciated.

Thanks

Will

Avatar
Hamish

Community Member, 712 Posts

6 August 2009 at 4:35pm

Are you writing the changes?

Put $member->write(); after the remove statement - that will apply the changes to your database.

Avatar
vancouverWill

Community Member, 121 Posts

7 August 2009 at 6:33am

haha. yeah you totally spotted what I missed for a couple of hours, thanks very much.

Will