We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    How would I go about creating a new empty container in a user's extended field?

    I used this
    <?php
    if ($profile = $modx->getObjectGraph('modUserProfile', array('User' => array()), array('fullname' => 'Sub Two'))) {
        $user = $profile->User;
        $extended = array();
        $extended = $profile->get('extended');
        $extended['subscriptions'][''] = '';
        $profile->set('extended', $extended);
        $profile->save();
    }
    return;

    and it did create a container with one empty attribute, but that empty attribute becomes attribute '0' with no value. I just want an empty container, with no attributes. In the database, if the container is created in the User editing form, it looks like this
    {"subscriptions":[]}

    This question has been answered by sottwell. See the first response.

      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
    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      To answer myself, I found this works fine. Create an empty extended attribute
      <?php
      if ($profile = $modx->getObjectGraph('modUserProfile', array('User' => array()), array('fullname' => 'Sub Two'))) {
          $user = $profile->User;
          $extended = array();
          $extended = $profile->get('extended');
          $extended['subscriptions'] = '';
          $profile->set('extended', $extended);
          $profile->save();
      }
      return;

      When you want to add an attribute to it, just treat it like an array, and it will at that point automatically become a container.
      <?php
      if ($profile = $modx->getObjectGraph('modUserProfile', array('User' => array()), array('fullname' => 'Sub Two'))) {
          $user = $profile->User;
          $extended = array();
          $extended = $profile->get('extended');
          $extended['subscriptions']['1234'] = 'timestamp';
          $profile->set('extended', $extended);
          $profile->save();
      }
      return;
        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