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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

onBeforeWrite & many-many problem


Reply


3 Posts   599 Views

Avatar
Captain America

Community Member, 2 Posts

27 October 2011 at 11:17pm

Hello,

I'm trying to change the default behaviour of member+group system, so that when member gets added to some specific group in backend (Security tab), he receives mail about it.

So if I'm right, the proper way to do this is through DataObjectDecorator and onBeforeWrite. Inside onBeforeWrite function I'm failing to get the old state of member groups, so I could compare which groups are new. Also something like $member->isChanged('Groups') is always false and I think it has something to do with many-many relation between member and group. Also putting die before parent::onBeforeWrite() shows me that new member groups have already been saved before onBeforeWrite have been excecuted, and normally "die" should preserve old state which it does on other fields, but not groups.

Similar question have already been asked here http://www.silverstripe.org/dataobjectmanager-module-forum/show/10548 but no solution found :( Does anyone know how I could get the old state inside onBeforeWrite or another way to accomplish this task?

Thnx!

Avatar
martimiz

Forum Moderator, 1106 Posts

28 October 2011 at 9:47am

Hi CA, welcome to the forums,

There's another thread on the forums that might help you further:

http://www.silverstripe.org/customising-the-cms/show/16155

Note: once a member has been created, he/she will never be 'new' again - not when deleted - they stay in the database and can be reactivated, not if you add them to another group.

Also: if you want to send the password in the mail, you'll only get the encrypted one... Read this thread:

http://www.silverstripe.org/general-questions/show/6925

Hope this helps some...

Avatar
Captain America

Community Member, 2 Posts

28 October 2011 at 11:46pm

Edited: 28/10/2011 11:48pm

Hey martimiz,

Thank you very much for your welcome and answer!

When I said "new" I didn't mean member, but his groups. Maybe I better demonstrate in code what I'm trying to acomplish. Imagine an already existing user in db that is in groups g1,g2,g3. Now in /admin/security I edit this user and also add him to groups g4,g5. Hitting save triggers onBeforeWrite...

class ExtendedMember extends DataObjectDecorator {   
   function onBeforeWrite() {
      // old groups -> g1,g2,g3,g4,g5 (but it should g1,g2,g3 because we're in
      // onBEFOREwrite and new groups shouldn't be written to yet)
      // is this even the right way to get old member's group state?
      $user = DataObject::get_by_id('Member', $this->owner->ID);
      $oldGroups = $user->Groups();
      
      // new user groups -> g1,g2,g3,g4,g5
      $newGroups = $this->owner->Groups();
      
      // after this compare $oldGroups and $newGroups to find out which groups have
      // been added / removed (g4,g5)
      // and send mail to user that he's been added to some new group

      parent::onBeforeWrite();
   }      
}

As you can see $oldGroups and $newGroups are the same :( I think it's because member<-->group is in many-many relation which gets updated even before onBeforeWrite. Any help is greatly appreciated! And sorry for bad english ;)