We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10378
    • 375 Posts
    How can I read and(!) write user settings in #MODx Revo 2.2.x directly without having a corresponding system setting?

    Read directly from user settings:

    $key = $modx->getOption('key', null, 0);
    


    ... always returns 0 - even so the setting "key" definitely exists in the actual user and contains a value. As I told, there is no related system setting with this key.

    Write directly to user settings:

    How can this be done?
    


    Thanks in advance,
    Martin

    This question has been answered by multiple community members. See the first response.

      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 10378
      • 375 Posts
      Sorry, I have found the solution in the meantime:

      Read user setting (uncached):

      $usersetting = $modx->getObject('modUserSetting', 'key');
      $key = $usersetting->get('value');
      


      Write user setting:

      $usersetting = $modx->getObject('modUserSetting', 'key');
      $usersetting->set('value', 113);
      $usersetting->save();
      
        Freelancer @bitego http://www.bitego.com
        ---
        GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
        More infos here: http://www.bitego.com/extras/goodnews/
      • discuss.answer
        • 3749
        • 24,544 Posts
        I don't think that's right. You haven't specified the user in either call, so unless you only have one user on the site, it isn't going to work properly.


        I think this is what you want in both cases:

        $key = 'SomeSettingKey';
        $userId = 'SomeUserID';
        $usersetting = $modx->getObject('modUserSetting', array('key' => $key, 'user' => $userId));
        

          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
          • 10378
          • 375 Posts
          Quote from: BobRay at May 28, 2013, 04:51 PM
          I don't think that's right. You haven't specified the user in either call, so unless you only have one user on the site, it isn't going to work properly.


          I think this is what you want in both cases:

          $key = 'SomeSettingKey';
          $userId = 'SomeUserID';
          $usersetting = $modx->getObject('modUserSetting', array('key' => $key, 'user' => $userId));
          


          Hi Bob,

          it's strange but reading the value works without the userID. And yes I have more then one users on the site. But thanks for your correction.
            Freelancer @bitego http://www.bitego.com
            ---
            GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
            More infos here: http://www.bitego.com/extras/goodnews/
          • discuss.answer
            • 42602
            • 81 Posts
            Reading works, but you'll get random row if you use getObject() without primary key "user". Result depends entirely on what row gets returned by your database platform. If you got 5 rows for key (5 different users) you'll most likely get the last updated row. But can't promise it smiley

            If you got multiple users and you want to update all, would be safer to use getCollection() to fetch all the settings with the key. I would suggest to use the bob's method as it is specific to user and will only return one row.

            If you have the user already with session setup and and authenticated. You can also call the settings in next manner:

            $settings = $modx->user->getSettings();
            $settings->set('key', $value);


            Settings table has row per key/user and they form primary key, so it will be unique row in that manner.
              • 10378
              • 375 Posts
              Thanks dunnock,
              I'll use Bob's method.
                Freelancer @bitego http://www.bitego.com
                ---
                GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                More infos here: http://www.bitego.com/extras/goodnews/
                • 3749
                • 24,544 Posts
                Quote from: gadgetto at May 29, 2013, 12:07 AM

                Hi Bob,
                it's strange but reading the value works without the userID. And yes I have more then one users on the site. But thanks for your correction.

                If you're in the front end and there's a logged-in user, getObject() may default to that user, but it's always safer to get the exact modUserSetting you want.
                  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