We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38925
    • 38 Posts
    Hi all !

    I'm creating an add-on to add users from a csv. But I'd like to check if the user I want to add already exists to avoid updating its password.

    Thank you !
      • 4172
      • 5,888 Posts
      if you know the username you can do something like that:

      if ($user = $modx->getObject('modUser', array('username'=>$username))){
      
         //do something with existing user
      
      }
      else{
          //add new user
          $user = $modx->newObject('modUser');
      
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 38925
        • 38 Posts
        It's exactly what I was looking for. Thank you wink
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Username field is unique so it will throw an exception if you try to insert a user that already exists. You should be able to use a try/catch exception handler to skip to the next record.
            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