On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • OK. I think I understand you.

    I’m going to guess that your real problem is that you need to create the usergroup and the user objects properly using xPDO so that they have the proper relations. And you need to add the members to the user groups properly too. Something like this:

    /* create a user group */
    $groupName = 'SomeName';
    
    $alreadyExists = $modx->getObject('modUserGroup',array('name' => $groupName));
    if ($alreadyExists) return $modx->error->failure($modx->lexicon('user_group_err_already_exists'));
    
    /* create group */
    $usergroup = $modx->newObject('modUserGroup');
    $usergroup->set('name',$groupName);
    $usergroup->save();
    
    /* add current user as a member */
    
    $member = $modx->newObject('modUserGroupMember');
            $member->set('user_group',$usergroup->get('id'));
            $member->set('member',$modx->user->get('id'));
            $member->set('role','some_role');
    
            $usergroup->addMany($member);
    
    
    /* save usergroup */
    if ($usergroup->save() == false) {
        return $modx->error->failure($modx->lexicon('user_group_err_create'));
    }


    Writing to the DB directly without going through xPDO is always risky and you can end up with confused relationships. Note: The stuff you did earlier may have left the DB in an unreliable state. It would be best to start with a fresh install.
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
    • Unfortunately, I could not start fresh install - site already works.
      So I will hope.

      One more question.

      Your code (adding user to existing group):
      $member = $modx->newObject('modUserGroupMember');
      $member->set('user_group',$usergroup->get('id'));
      $member->set('member',$modx->user->get('id'));
      $member->set('role','some_role');
      
      $usergroup->addMany($member);


      How to remove user from group using same syntax?

      Thanks.
      • I’m not positive, but I think it would be something like this:

        <?php
        /* get the user and group objects (unless you already have them) */
        $user = $modx->getObject('modUser',array('name'=>'JoeBlow'));
        $groupr = $modx->getObject('modUserGroup',array('name'=>'Editors'));
        
        $userId = $user->get('id');
        $groupId = $group->get('id');
        
        $groupMember = $modx->getObject('modUserGroupMember', array('user_group'->$groupId,'member'=>$userId));
        $groupMember->remove();
        ?>


          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
        • Thanks. It works.
          • I hoped it would.

            Glad you got is sorted. Thanks for reporting back. smiley
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting