We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29606
    • 63 Posts
    Not sure if I should post this here or in xPDO...

    I want to add a custom field (an integer) to an existing object (the modx user object).
    How should I do this ?

    For a better understanding of my request, I want to link my modx users to another user database, with the same users and infos, but they may have different ID.
    So I’d like MODx to be abble to know the ID of the current user in the foreign database, so that I could do :

    $user= $modx->getUser();
    $userID2=$user->get('id2');
      • 21257 MODX Staff
      • 730 Posts
      One way would be to extend the User class, but given your requirements, I think the simpler approach would be to utilize "User Settings", represented by the UserSetting class. (If you edit a user in Revolution and click on the Settings tab, you’ll see a table of these custom settings .)
      How you get at them programatically is pretty simple. The User class and the UserSetting class are already related in the MODx model.

      To get a collection of existing UserSetting objects:
      $settings= $modx->user->getMany('UserSetting'); // this was modUserSetting before beta3 - thanks splittingred
      


      Note that $modx :: getUser already does the above, and stores the key/value pairs of all the UserSettings in $modx->_userConfig

      To create a new UserSetting object, associate it with a particular User, then save it:
      // create the user setting
      $setting =& $modx->newObject('UserSetting');
      $setting->set('user', $user_id); // actually I don't think this line has to be included if you associate the $setting with a modUser as below.. please correct me if I'm wrong anyone
      $setting->set('key', 'the_key');
      $setting->set('value', 'the_value');
      
      // associate it with a User
      $user->addOne($setting);
      
      // save it
      $setting->save();
      // .. or this should also work..
      // $user->save();
      


      edit: See discussion below for information on accessing the UserSettings via $modx::getOption and reloading the config before doing so.
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 22303 MODX Staff
        • 10,725 Posts
        Note that all user settings are automatically loaded on each request, so once you set a setting it is loaded into the $modx->config array, and this it will be available via $modx->getOption(’setting_name’);
          • 29606
          • 63 Posts
          Ok, I managed to create the setting using netProphET code, and it appears in the user settings alright.

          However, I can’t find a way to recover the value :
          $modx->user->getMany(’modUserSetting’) returns an empty array,
          $modx->getOption(’setting_name’) returns nothing
          $user->get(’setting_name’) returns nothing either...

          Even settings I created under the user - setting tab do not appear.
          $modx->_userConfig() only returns settings created under the user - setting tab.

          Plus, I noticed that if I try to set again a setting with the same key but a different value, the setting keeps the old value.

          I guess I’m still not ready to harness the power of MODx...
            • 28215
            • 4,149 Posts
            Quote from: Thalion at Aug 11, 2009, 02:19 PM

            $modx->user->getMany(’modUserSetting’) returns an empty array,
            Are you using the latest SVN or beta-3? In SVN, this alias has been changed to ’UserSetting’.

            $modx->getOption(’setting_name’) returns nothing
            Is the current logged in user the one that the setting is assigned to? This will only work if that is the case.

            $user->get(’setting_name’) returns nothing either...
            Well of course not; get() returns field names.

            Have you tried clearing the cache, as well?


            Plus, I noticed that if I try to set again a setting with the same key but a different value, the setting keeps the old value.

            I guess I’m still not ready to harness the power of MODx...
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 29606
              • 63 Posts
              I’m using SVN 5627, I don’t know if the change happened before or after ? Either way, $modx->user->getMany(’UserSetting’) is empty as well...

              Clearing the cache doesn’t change anything
                • 21257 MODX Staff
                • 730 Posts
                @splittingred How problematic is it to be logged in as two different users at once? How I understood it last, which is a few months ago now, is that the User model was essentially agnostic of the ’Manager User’ vs. ’Web User’ concept, but it seemed that the distinctions remained at the session level.
                  Mike Schell
                  Lead Developer, MODX Cloud
                  Email: [email protected]
                  GitHub: https://github.com/netProphET/
                  Twitter: @mkschell
                  • 28215
                  • 4,149 Posts
                  Quote from: netProphET at Aug 11, 2009, 02:59 PM

                  @splittingred How problematic is it to be logged in as two different users at once? How I understood it last, which is a few months ago now, is that the User model was essentially agnostic of the ’Manager User’ vs. ’Web User’ concept, but it seemed that the distinctions remained at the session level.

                  Theoretically (Jason will have to confirm) it shouldn’t be problematic at all. Sessions are separated into Contexts, so having different logins in different Contexts wont be a problem.

                  Jason can confirm that, though.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 28215
                    • 4,149 Posts
                    Quote from: Thalion at Aug 11, 2009, 02:50 PM

                    I’m using SVN 5627, I don’t know if the change happened before or after ? Either way, $modx->user->getMany(’UserSetting’) is empty as well...

                    Can you give me a bit more info?

                    1. The key of the setting you’re grabbing
                    2. Are you accessing this in the web context? Or in a different one?
                    3. How are you trying to access this setting? Can you post code?
                    4. What user is this setting being attached to? Your user, or another?
                      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                      • 29606
                      • 63 Posts
                      I try to create, then grab a key called "SMF_ID"
                      I want to do this with a snippet called in the Web Context
                      For the moment, I test this on my local xampp installation, with my own user account (the one created when I installed MODx), but I did test with other users without luck.

                      Here is the code of my snippet :
                      <?php
                      if ($modx->getLoginUserName($modx->context->key))
                      {
                        $user= $modx->getUser();
                        $id=$user->get('id');
                      
                        $setting =$modx->newObject('modUserSetting');
                        $setting->set('user', $id);
                        $setting->set('key', 'SMF_ID');
                        $setting->set('value', '1');
                        // associate it with a modUser
                        $user->addOne($setting);
                        // save it
                        $setting->save();
                      }
                      $verif=$modx->getOption('SMF_ID');
                      return print_r($verif,true);
                      ?>