We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22019
    • 390 Posts
    I’m trying to add a new value to an extended setting for a user - I can add it in the right place, but it does this by replacing the existing array, rather than appending to it.
    I want to store multiple copies of the same variable name in a container - so I’ve done that rather crudely with a counter. On save it replaces the existing container array with my new one, but with the ’correct’ counter number.

    Here’s what I’m using:

    $profile = $user->getOne('Profile');
       $oldextended = $profile->get('extended');
       $container_array = $oldextended['container'];
       $n = count($container_array);
       $newextended = array('container' =>array ('id_'.($n+1) => $value));
       $extended = array_merge($oldextended,$newextended);
       $profile->set('extended', $extended);
       if (!$profile->save()) {
        
        // Houston we have a problem
    }
    


    I’ve tried various different array_push and array_merge_recursive statements, but I can’t seem to get more than one item in my container array. Advice?
      Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
      • 3749
      • 24,544 Posts
      I don’t see why you’re creating two extra arrays.

      Wouldn’t this do it?

      <?php
      $oldextended = $profile->get('extended');
      $count = count($oldextended['container']);
      $count++;
      $oldextended['container']['id_' . $count] = $value;
      $profile->set('extended', $oldextended);
      $profile->save();


      FWIW, I know this works:

      <?php
      $user = $modx->getObjectGraph('modUser',array('Profile' => array()),array('username'=>'JaneDoe'));
      $extFields = $user->Profile->get('extended');
      $extFields['sign'] = 'Virgo';
      $user->Profile->set('extended',$extFields);
      $user->save();
      


      Make sure that the value you’re setting isn’t null (or possibly empty). The last time I checked, that caused trouble.
        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
        • 22019
        • 390 Posts
        Thanks Bob. As you can tell, I’m a relative novice at PHP - from reading around it seems it would be much simpler to create another array within the ’container’ array named after the key I want, and then simply use $array[]=$value to append the new $value at the end.

        I’d got myself in a muddle by trying to reuse the code for the UpdateProfile snippet, which is aimed at doing a slightly different job.

        What I still don’t know is whether it would be better to extend modUser instead, or even ignore both of these methods and use a custom table with remote-key in the user table as a foreign key. I guess if I had a clearer idea what I need the data for, rather than simply collecting the blessed stuff, that would help.

        Hopefully your book will help to shed some light on this kind of ’MODx gives you N ways of achieving Y’ conundrum.... grin
          Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
          • 3749
          • 24,544 Posts
          Quote from: odeclas at Jan 03, 2011, 01:20 PM

          Thanks Bob. As you can tell, I’m a relative novice at PHP - from reading around it seems it would be much simpler to create another array within the ’container’ array named after the key I want, and then simply use $array[]=$value to append the new $value at the end.

          I’d got myself in a muddle by trying to reuse the code for the UpdateProfile snippet, which is aimed at doing a slightly different job.

          What I still don’t know is whether it would be better to extend modUser instead, or even ignore both of these methods and use a custom table with remote-key in the user table as a foreign key. I guess if I had a clearer idea what I need the data for, rather than simply collecting the blessed stuff, that would help.
          First, I definitely don’t claim to be a PHP wizard (though I do know some). wink

          It’s always hard to make those decisions. I can’t really help because I don’t know what it is you’re saving, how many different items you’re saving over time, and what the possible values and speed considerations are.

          You might want a custom table (although you definitely don’t want to be altering the user table in the process), but offhand, I would say probably not unless the data for each user will continue to grow and grow large over time.

          Just a note about creating a container[] array. Remember that the user is an array containing the profile which is an array containing the extended array, which will then contain your container array. There’s nothing at all wrong with that, but it can make it hard to keep things straight when you’re programming, especially if you’re not used to nested arrays.

          Here’s another way to do what you originally had (I think this is what you were describing):

          <?php
          $oldextended = $profile->get('extended');
          $count = count($oldextended['container']) +1;
          $oldextended[] = 'container' . $count] => $value;
          $profile->set('extended', $oldextended);
          $profile->save();


          Another way to go if you’re just storing a list of IDs that have some relevance to the user is to make a comma-delimited string out of your data items, add new values on the end, and use exlode() when you want to get all values and strstr() (or explode() and in_array() ) when you want to test for one:

          Adding a value would look like this:
          <?
          $oldextended = $profile->get('extended');
          $newValue = 'someValue'
          $oldextended['container'] = $oldextended['container'] . ',' . $newvalue;
          $oldextended->save();


          I like this method and it’s used often in MODx. It’s easier to debug, and it saves on unnecessary array overhead and indexing.
            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
            • 22019
            • 390 Posts
            Thanks for that further option.

            I’m storing the subscription id that has been generated for a user, and rather than simply store the active subscription I thought I might as well store all of them. The subscription id could be used as a FK on the subscriptions table, so I guess I should be trying to store it in a way that causes the least fuss if/when I create a CMP for user subs. I guess, now that I really think about it, I should store the user_id in the subscriptions table, and not the subscription_id in the user table.

            (ie do the same as Shaun has done with FileLister downloads)
              Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86