We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Sorry for the delay. I've been thinking about this. I'm still not sure what's happening, but I think there are two things at work. One is that because it's the Register snippet, at the time my code runs, the user hasn't been created yet, so there's no real ID available. The 'id' field in the ext_user_data table is arbitrary and it's value isn't used anywhere. The userdata_id is critical, though, and should correspond to the user's ID in the modx_users table. Putting the snippet below the Register snippet won't work because Register redirects the user and the data would be lost.

    The second thing is that I expected that the userdata_id would be set to 0, but I think it's being set to 1 because you're logged into the Manager and that's your ID. I could be wrong.

    A possible solution is to run the snippet as a Register postHook (possibly with &persistParams=`1`), but it would have to be rewritten to get the data with $hook->getValue(). I think it would work, but I'm not sure. Unfortunately, I don't have time to mess with it right now.

    It might also be possible to put the snippet on the "thanks for registering" page with &persistParams=`1`. At that point the user should exist, but I'm not sure what &persistParams actually does.

    Remember that my original snippet is designed to update an existing user's profile, so $modx->user would always give access to the user's ID.

    BTW, the record not being deleted when you delete a user is normal because the modUser object doesn't know about the custom table. The record has to be removed in a plugin attached to OnUserRemove. I think this would be the code (assuming that the userdata_id fields are correct):

    $userId = $user->get('id');
    $extUser = $modx->getObject('userData',array('userdata_id' => $userId));
    if ($extUser) {
       $extUser->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
      • 49659
      • 86 Posts
      Hi Bob. I got it working! I have to go now. Will send details tomorrow.

      Best wishes. Karl
        • 3749
        • 24,544 Posts
        I'm glad you got it sorted. Sorry it took so long.
          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
          • 49659
          • 86 Posts
          Using your advice and indications, I got it working as follows:

          1) Used &postHooks=`ExtUserRegister`:

          [[!Register?
          &submitVar=`loginRegisterBtn`
          ...
          &useExtended=`0`
          &postHooks=`ExtUserRegister`
          ]]


          2) In the 'ExtUserRegister' snippet, retrieved 'username':

          $userName = $hook->getValue('username');


          3) Looked up 'id' using 'username':

          $c = $modx->newQuery('modUser');
          $users = $modx->getCollection("modUser",$c);

          foreach($users as $newUser){
          if ($userName == $newUser->get('username')) {
          $userID = $newUser->get('id');
          break;
          }
          }


          4) Set 'userdata_id' to 'id':

          $data = $modx->getObject('userData',
          array('userdata_id' => $userID), false);
          if ($data) {
          $fields = $data->toArray();
          } else {
          $data = $modx->newObject('userData');
          if ($data) {
          $data->set('userdata_id', $userID);
          $fields = $data->toArray();
          }
          }


          5) Modify return value from '' to true (I don't know if this was necessary)


          NOTE: In step 3, with just a few users, it works fine and quickly to iterate through them to get the 'id', but might there be a faster way to get the user id, for when there are several hundred or thousand users to select from?
            • 3749
            • 24,544 Posts
            Yes, this should get just the user you want:

            $usr = $modx->getObject("modUser",array('username' => $userName));


            How are you getting the value of the $data variable?

            I think step 5 is necessary, otherwise Register won't run any subsequent hooks and can't forward the user.
              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
              • 49659
              • 86 Posts
              Hi Bob,

              Thanks for your help sorting this out. Here's the entire working 'ExtUserRegister' snippet, which is based on your 'ExtUserUpdateProfile' snippet:


              //$modx->lexicon->load('login:updateprofile');

              $submission = isset($_POST['loginRegisterBtn']) && ($_POST['loginRegisterBtn'] == 'Register');

              $data = null;
              $newUser = null;
              $userName = null;
              $userID = null;
              $fields = array();

              /* @var $data userData */

              if (isset($modx->user) && ($modx->user instanceof modUser)) {

              $userName = $hook->getValue('username');

              //START GET NEW USER ID:
              $newUser = $modx->getObject("modUser",array('username' => $userName));
              $userID = $newUser->get('id');
              //END GET NEW USER ID

              $data = $modx->getObject('userData',
              array('userdata_id' => $userID), false);
              if ($data) {
              $fields = $data->toArray();
              } else {
              $data = $modx->newObject('userData');
              if ($data) {
              $data->set('userdata_id', $userID);
              $fields = $data->toArray();

              }
              }
              }

              if (! is_array($fields) || empty($fields)) {
              return '';
              }

              /* Convert any nulls to '' */
              if (!empty($fields)) {
              foreach($fields as $key => $value) {
              if (empty($value) && ($value !== '0')) {
              $fields[$key] = '';
              }
              }
              $modx->setPlaceholders($fields);
              }

              if ($submission) {
              $modx->request->sanitizeRequest();
              $dirty = false;

              foreach ($fields as $key => $value) {
              if (isset($_POST[$key])) {
              if ($value !== $_POST[$key]) {
              $data->set($key, $_POST[$key]);
              $dirty = true;
              }
              }
              }

              if ($dirty) {

              if( $data->save()) {
              $msg = '[ExtUserRegister] Saved successfully';
              $modx->log(modX::LOG_LEVEL_ERROR, $msg);
              } else {
              $msg = '[ExtUserRegister] Save failed';
              $modx->log(modX::LOG_LEVEL_ERROR, $msg);
              }
              }
              }

              return true;
              • discuss.answer
                • 3749
                • 24,544 Posts
                Thanks Karl. smiley

                I'll add it to the next release (with credit to you, of course). Here it is with slightly modified error handling and other minor changes. Let me know if it still works. wink

                $submission = isset($_POST['loginRegisterBtn']) && ($_POST['loginRegisterBtn'] == 'Register');
                
                $data = NULL;
                $newUser = NULL;
                $userName = NULL;
                $userID = NULL;
                $fields = array();
                
                /* @var $data userData */
                
                if (isset($modx->user) && ($modx->user instanceof modUser)) {
                
                    $userName = $hook->getValue('username');
                
                   /* Get new user ID via username */
                    $newUser = $modx->getObject("modUser", array('username' => $userName));
                    $userId = $newUser->get('id');
                
                
                    $data = $modx->getObject('userData',
                        array('userdata_id' => $userId), false);
                    if ($data) {
                        $fields = $data->toArray();
                    } else {
                        $data = $modx->newObject('userData');
                        if ($data) {
                            $data->set('userdata_id', $userId);
                            $fields = $data->toArray();
                        }
                    }
                }
                
                if (!is_array($fields) || empty($fields)) {
                    $hook->addError('username', '[ExtUserRegisterPosthook] Error getting user data');
                    return false;
                }
                
                /* Convert any nulls to '' */
                if (!empty($fields)) {
                    foreach ($fields as $key => $value) {
                        if (empty($value) && ($value !== '0')) {
                            $fields[$key] = '';
                        }
                    }
                    $modx->setPlaceholders($fields);
                }
                
                if ($submission) {
                    $modx->request->sanitizeRequest();
                    $dirty = false;
                
                    foreach ($fields as $key => $value) {
                        if (isset($_POST[$key])) {
                            if ($value !== $_POST[$key]) {
                                $data->set($key, $_POST[$key]);
                                $dirty = true;
                            }
                        }
                    }
                
                    if ($dirty) {
                        if ($data->save()) {
                            // $msg = '[ExtUserRegisterPosthook] Saved successfully';
                            // $modx->log(modX::LOG_LEVEL_ERROR, $msg);
                        } else {
                            $msg = '[ExtUserRegisterPosthook] Save failed';
                            $hook->addError('username', $msg);
                            return false;
                        }
                    }
                }
                
                return true;
                  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
                  • 49659
                  • 86 Posts
                  Hi Bob,

                  It works great. Thank you so much for your help!

                  Karl
                    • 38783
                    • 571 Posts
                    This post just saved me!

                    After a couple of hours experiencing exactly the problem described here I read this and it nudged me in the right direction.

                    I realised I had forgotten to add
                    &useExtended=`0`
                    to my Update Profile page.

                    Thank you Bob and Karl
                      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                      email: [email protected] | website: https://andytough.com