We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10152
    • 156 Posts
    Hi Im attempting to store values for each member.

    For native modx fields there is no problem :
    ...
    $zip = $modx->getOption('default_zipcode') ;      
    $profile->set('zip',$zip);     
    ...
    


    But how to write user settings ?
    $profile->setOption('accept_newsletter','yes');  // NOT OK
    

    $modx->setOption('accept_newsletter','yes'); // NOT OK
    


    I tried unsuccessfuly to adapt the example from the doc and performed a quick search in Bob Ray’s guide but could not locate answers.
    Thanks for any advice
      • 22303 MODX Staff
      • 10,725 Posts
      There is no setOption() method. You have to create a modUserSetting instance with all the details:
      <?php
      $accept_newsletter = $modx->newObject('modUserSetting');
      $accept_newsletter->set('key', 'accept_newsletter');
      $accept_newsletter->set('value', '1');
      $accept_newsletter->set('xtype', 'boolean');
      $accept_newsletter->set('namespace', 'myNewsletterNS');
      $accept_newsletter->set('area', 'MyNewsletterArea');
      
      // use addMany() because settings are the many in a one-to-many relationship from user
      $user->addMany($accept_newsletter);
      
      $user->save();
      ?>

      When you create a setting in the manager you can see that the valid xtype values can be textfield, textarea, or boolean. Remember that booleans are represented by a string ’1’ or string ’0’, since all values for settings are actually stored in a text data field.

      Please note however, that although user settings are actually available from $modx->getOption(), this is loaded via a special cache and session mechanism related to MODx Contexts and adding this data object will not make the setting available via $modx->getOption() until the user session is updated.

      Settings may not be what you want here. A new feature is being added (it already exists in the table and API in RC2) that will allow you to store and access unlimited extended user profile attributes as part of the core modUserProfile. I’ll have more information soon, but it would work like this to add a new attribute:
      <?php
      // get the array of extended user profile attributes
      $extended = $profile->get('extended');
      
      // add/replace the value of the element for key `accept_newsletter`
      $extended['accept_newsletter'] => true;
      
      $profile->set('extended', $extended);
      $profile->save();
      ?>

      The PHP array you set() in the extended field is encoded to a JSON object before being stored in the database, and a fulltext index is applied to the field so it should be searchable in various ways if necessary. The array can be as simple or complex as you want (though don’t nest too deeply or managing the data will become messy), and a tab on the user management interface will be available in the core manager distribution for this data before 2.0 goes GA.
        • 10152
        • 156 Posts
        thank you for these detailled informations.

        User settings are interesting because they allow combo-boolean xtype and could make it intuitive for the site owner to change user settings.

        To avoid handling custom tables several profile fields have already been merged in the user_attributes tables ( fullname : "Firstname|LastName|Youngbridename" ).

        I am impatient to check the extended field possibilities.

        So I presume there will be new fields (select lists, text, textarea, checkboxes...) added to the manager ( automatically ? ) , very nice !

          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: French at Jun 06, 2010, 12:22 PM

          User settings are interesting because they allow combo-boolean xtype and could make it intuitive for the site owner to change user settings.
          Yes, but not so good for profile editing or controlling dynamic site features on the fly. It could be expensive to rebuild the user settings.

          Quote from: French at Jun 06, 2010, 12:22 PM

          To avoid handling custom tables several profile fields have already been merged in the user_attributes tables ( fullname : "Firstname|LastName|Youngbridename" ).
          That will not be supported; you should never modify a core MODx table.

          Quote from: French at Jun 06, 2010, 12:22 PM

          I am impatient to check the extended field possibilities.

          So I presume there will be new fields (select lists, text, textarea, checkboxes...) added to the manager ( automatically ? ) , very nice !
          These will not have xtypes; these will be simple attributes, likely treated all as text in the interface. You can see an example in the User editor now in the Extended tab. If you want to create dropdowns and other custom form inputs for the data, you’ll have to create such an interface manually.
            • 10152
            • 156 Posts
            Thank you !