Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » want to remove members from group but no errors nor any results

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

Page: 1
Go to End
Author Topic: 1158 Views
  • vancouverWill
    Avatar
    Community Member
    121 Posts

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

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: want to remove members from group but no errors nor any results Link to this post

    Are you writing the changes?

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

  • vancouverWill
    Avatar
    Community Member
    121 Posts

    Re: want to remove members from group but no errors nor any results Link to this post

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

    Will

    1158 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.