We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    Is runProcessor cached when executed within a loop? I'm trying to create multiple new user accounts by running 'security/user/create' inside a foreach loop, however it only creates the first user.

    The error output just repeats the details of the first user inserted for every other record.

    // $newusers = array containing new users (all required fields are set)
    
    foreach ($newusers as $user) {
    
        $props = array(
            'newpassword' => true,
            'passwordnotifymethod' => 's',
            'passwordgenmethod' => 'g',
        );
        $props = array_merge($user,$props);
             
        $response = $modx->runProcessor('security/user/create', $props);
        $errors = $response->getAllErrors();
        $output .= implode("\n",$errors);
    }
    


    I guess I could use xpdo to create the users but that seems pointless when a perfectly good processor is already in place...
      • 3749
      • 24,544 Posts
      Are the $users created with $modx->newObject()? If so, they're objects, not arrays, so you'd need this:

      $props = array_merge($user->toArray(),$props);


      Also, you might need to give each user a different ID and add this to your $props array:

      'class_key' => 'modUser'




      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 33968
        • 863 Posts
        Hi Bob, no $users was multi-dimensional array containing all the new user fields to be imported. They matched up to the new user object fields required. The class key was set as well.

        I couldn't end up getting this to work so just created the new users/usergroups/ etc using xpdo. It would have been much quicker and more solid if the processor had worked though!

        Cheers
          • 3749
          • 24,544 Posts
          Hmm, here's the code from the Register snippet processor:


          /* create user and profile */
                  $this->user = $this->modx->newObject('modUser');
                  $this->profile = $this->modx->newObject('modUserProfile');



          So, maybe the user/create processor is not ready for prime time. I can't find any example of it being called in the core code.


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html

            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
            • 33968
            • 863 Posts
            The processor is run from the manager when creating a new user; it works fine. The problem here is with executing runProcessor() inside a loop.

            Anyway, I've done what I need to do for now but will investigate with other processors when I have some more time.
              • 3749
              • 24,544 Posts
              Looking at the code, I don't see any reason why it shouldn't work in a loop (not that that helps you any). wink runProcessor() always calls setProperties() with the $scriptProperties array.



              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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
                • 33968
                • 863 Posts
                Yeah, odd isn't it? Anyway, thanks for having a look Bob... will update this if I find anything more.