We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42049 ☆ A M B ☆
    • 250 Posts
    Im building up a user, adding profile information and then adding them to a spcific group e.t.c, but after they are saved, I want to find out there specific user id.

    I have also tried $modx->lastInsertId() but it returns a number which doesnt match up with the user ID i see in the manager...

    My code...

    	//Init our modx manager
    	$modx->initialize('mgr');
    
    	// Fetch user group first
    	$group = $modx->getObject('modUserGroup',array('name'=>'Clients'));
    
    	// Create User
    	$user = $modx->newObject('modUser', array('username' => $modxData['username']));
    	$user->set('password', md5($modxData['password']));
    	
    	//Create Users Profile
    	$profile = $modx->newObject('modUserProfile');
    	$profile->set('fullname', $modxData['firstName'] . ' ' . $modxData['lastName']);
    	$profile->set('email', $modxData['email']);
    	$profile->set('address', $modxData['address'] . ' ' . $modxData['county'] . ' ' . $modxData['postcode']);
    	$profile->set('phone', $modxData['contactNumber']);
    	
    	//Add profile to user
    	$user->addOne($profile);
    
    	// Set the role
    	$role = $modx->getObject('modUserGroupRole', array('name'=>'Member', 'authority'=>'9999'));
    
    	// Add the user to the group with the role above
    	$intersect = $modx->newObject('modUserGroupMember');
    	$intersect->addOne($user);
    	$intersect->addOne($group);
    	$intersect->addOne($role);
    	$modx->beginTransaction();
    
    	if (! $intersect->save()) {
    		$modx->rollback();
    		return header('HTTP/1.1 406 Error - Something went wrong whilst trying to add you into MODx');
    	} else {
    		$modx->commit();
    
    		//user id of inserted member please!
    	}
      GEL STUDIOS
      MODX Professional | MODX Ambassador

      Website | Email | Twitter | Facebook
      • 42049 ☆ A M B ☆
      • 250 Posts
      EDIT found it, as i knew the username.

      However is there an easier way?

      $user=$modx->getObject('modUser',array('username'=>$modxData['username']));
      echo $user->get('id');
      
        GEL STUDIOS
        MODX Professional | MODX Ambassador

        Website | Email | Twitter | Facebook
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        The "id" field actually only identifies the user in the "users" table; in all of the other user tables the "id" field refers to the record (which may or may not be in sync with the users table), so the actual user ID is stored as internalKey.

        If you are doing whatever you are doing immediately after saving the user, I'm pretty sure there is a way of accessing the OnUserSave event in a snippet, or at least I'm pretty sure I've seen such a thing in code somewhere - in any case it provides an object of modUser with all the user data.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 16348
          • 64 Posts
          You should also be able to retrieve the user id with:
          ...
          $modx->commit();
          $user->getPrimaryKey();
          

          The field is updated upon save.
            • 22303 MODX Staff
            • 10,725 Posts
            First, I wouldn't bother with the transaction code; in order for the related object queries to work with transactions, PDO would have to support nested transactions, which I don't believe it does.

            Second, as soon as you call save() on an xPDOObject, the primary key is populated on the object. You do not need to do anything but access the primary key field( s ) as you normally would.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org